Last active
July 31, 2018 19:55
-
-
Save monochromer/31578bd461acf10f29256c83d0a7c354 to your computer and use it in GitHub Desktop.
загрузка шрифтов через FontFaceObserver.
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
let fontFace = new FontFace("MyWebFont", "url('MyWebFont.woff2') format('woff2'), url('MyWebFont.woff') format('woff')"); | |
fontFace.load().then(function(loadedFontFace) { | |
document.fonts.add(loadedFontFace); | |
document.getElementById("target").style.fontFamily = "MyWebFont"; | |
}); |
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
;(function(window, document, undefined) { | |
'use strict'; | |
var fonts = [ | |
{ | |
name: 'Open Sans', | |
weight: 'normal', | |
style: 'normal' | |
}, | |
{ | |
name: 'Open Sans', | |
weight: 'normal', | |
style: 'italic' | |
}, | |
{ | |
name: 'Open Sans', | |
weight: 'bold', | |
style: 'normal' | |
}, | |
{ | |
name: 'Open Sans', | |
weight: 'bold', | |
style: 'italic' | |
} | |
]; | |
var html = document.documentElement; | |
html.classList.add('fonts-loading'); | |
if (sessionStorage.fontsLoaded) { | |
html.classList.remove('fonts-loading'); | |
html.classList.add('fonts-loaded'); | |
return; | |
} | |
Promise | |
.all(fonts.map(function(font) { | |
return (new FontFaceObserver(font.name, { | |
style: font.style, | |
weight: font.weight | |
})).load(); | |
})) | |
.then(function () { | |
html.classList.remove('fonts-loading'); | |
html.classList.add('fonts-loaded'); | |
sessionStorage.fontsLoaded = true; | |
}) | |
.catch(function () { | |
html.classList.remove('fonts-loading'); | |
html.classList.add('fonts-failed'); | |
sessionStorage.fontsLoaded = false; | |
}); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment