Categories: CSSScriptWeb Design

Lazy Load Fonts with IntersectionObserver API – LoadFont.js

LoadFont is a lightweight JavaScript utility that loads a font file for a specific DOM element when it enters the viewport.

The library handles font loading through the Intersection Observer API. It loads fonts only when their associated elements become visible.

This approach reduces initial page load times and prevents unnecessary network requests for fonts that users might never see.

How to use it:

1. Download the package and load the necessary modules:

import { lazyLoadFont, loadFont } from './loadfont.js';

2. Set up lazy loading for your target elements:

// Target elements by CSS selector
lazyLoadFont('.custom-font-element');

// Or target a specific DOM element
const element = document.querySelector('#special-heading');
lazyLoadFont(element);

3. Add the required data- attributes to your HTML element. The data-lf-url is the path to your font file, and data-lf-name is the font-family name you’ll use in your CSS.

<div 
  class="custom-font-element" 
  data-lf-url="fonts/custom-font.woff2" 
  data-lf-name="CustomFont">
  Your custom text here
</div>

4. LoadFont only handles the loading of the font file; it doesn’t apply it. You still need to assign the font-family in your stylesheet as you normally would.

.custom-font-element {
  font-family: CustomFont, fallback-font, sans-serif;
  opacity: 0; /* Hide until font loads */}

.custom-font-element[data-lf-status='2'] {
  opacity: 1; /* Show when loaded */}

5. Configure the IntersectionObserver:

lazyLoadFont('.custom-font-element',{
  // Intersection_Observer_API options here
});

The post Lazy Load Fonts with IntersectionObserver API – LoadFont.js appeared first on CSS Script.

rssfeeds-admin

Share
Published by
rssfeeds-admin

Recent Posts

Spider-Noir Trailer Sets the Stage for 1930s Mystery and Superpowered Goons

Sony Pictures and Amazon’s Prime Video have published an official trailer for their Spider-Noir show,…

31 minutes ago

Star Trek: Strange New Worlds Season 4 Premiere Set for July 2026

Star Trek: Strange New Worlds Season 4 will premiere on Paramount+ on Thursday, July 23,…

2 hours ago

Hazbin Hotel Confirmed to End With Season 5 Before Season 3 Even has a Release Date

Vivienne Medrano’s adult animation hit, Hazbin Hotel, will come to an end with Season 5,…

4 hours ago

New Windows RPC Vulnerability Lets Attackers Escalate Privileges Across All Windows Versions

PhantomRPC, a newly identified architectural vulnerability in Windows Remote Procedure Call (RPC) that enables local…

6 hours ago

CISA Warns of Multiple SimpleHelp Vulnerabilities Exploited in Attack

The Cybersecurity and Infrastructure Security Agency (CISA) has issued a critical alert regarding two actively…

6 hours ago

Gina Carano Teases Potential Star Wars Return After ‘Really Nice’ Conversation With Lucasfilm Following Disney Lawsuit

It seems a return to Star Wars could be in the cards for Gina Carano…

7 hours ago

This website uses cookies.