Created
October 8, 2019 23:24
-
-
Save cvan/8e8f1adc2112cb4f03039f0c656de7c4 to your computer and use it in GitHub Desktop.
loading fonts with JavaScript using `new FontFace` (to avoid FOUF)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function () { | |
async function loadFonts () { | |
if (!('FontFace' in window)) { | |
return; | |
} | |
const fonts = [ | |
new FontFace('theinhardt', 'url(/fonts/akzidenz-grotesk/AkzidenzGrotesk-Light.woff2)'), // 400 | |
new FontFace('theinhardt', 'url(/fonts/akzidenz-grotesk/AkzidenzGrotesk-Regular.woff2)'), // 500 | |
new FontFace('theinhardt', 'url(/fonts/akzidenz-grotesk/AkzidenzGrotesk-Medium.woff2)'), // 600 | |
]; | |
Promise.all(fonts).forEach(fontsLoaded => { | |
fontsLoaded.forEach(font => { | |
await font.load(); | |
document.fonts.add(font); | |
}); | |
document.body.classList.add('fonts-loaded'); | |
}); | |
} | |
await loadFonts(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment