Skip to content

Instantly share code, notes, and snippets.

@monochromer
Last active July 31, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monochromer/31578bd461acf10f29256c83d0a7c354 to your computer and use it in GitHub Desktop.
Save monochromer/31578bd461acf10f29256c83d0a7c354 to your computer and use it in GitHub Desktop.
загрузка шрифтов через FontFaceObserver.
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";
});
;(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