当前位置: 首页 > news >正文

fontfaceobserver 第三方字体加载优化方案

fontfaceobserver 第三方字体加载优化方案

1. github地址

https://github.com/bramstein/fontfaceobserver

2. 基础使用方法

const font = new FontFaceObserver('My Family', {weight: 400
});font.load().then(function () {console.log('Font is available');
}, function () {console.log('Font is not available');
});
  1. FontFaceObserver:两个参数,第一个font-family必传,第二个option对象,可设置weight、style、stretch等属性;
  2. load方法:开始下载字体返回promise,字体加载成功和失败时分别转为成功和失败态,可在加载字体完成后去使用字体。
  3. load参数,接受两个参数,第一个参数为字符串或null,如果你的字体不包含拉丁字符,你可以通过该参数自定义测试字符串。第二个参数表示加载超时时长,默认3秒,3秒每加载成功即表示字体加载失败,单位为毫秒。
var font = new FontFaceObserver('My Family');font.load(null, 5000).then(function () {console.log('Font is available');
}, function () {console.log('Font is not available after waiting 5 seconds');
});

3. 并行加载多种字体

var fontA = new FontFaceObserver('Family A');
var fontB = new FontFaceObserver('Family B');Promise.all([fontA.load(), fontB.load()]).then(function () {console.log('Family A & B have loaded');
});
var exampleFontData = {'Family A': { weight: 400, color: 'red' },'Family B': { weight: 400, color: 'orange' },'Family C': { weight: 900, color: 'yellow' },// Etc.
};var observers = [];// Make one observer for each font,
// by iterating over the data we already have
Object.keys(exampleFontData).forEach(function(family) {var data = exampleFontData[family];var obs = new FontFaceObserver(family, data);observers.push(obs.load());
});Promise.all(observers).then(function(fonts) {fonts.forEach(function(font) {console.log(font.family + ' ' + font.weight + ' ' + 'loaded');// Map the result of the Promise back to our existing data,// to get the other properties we need.console.log(exampleFontData[font.family].color);});}).catch(function(err) {console.warn('Some critical font are not available:', err);});
  1. 字体加载前,声明字体
const createFontFace2HTMLByTTF = (fontFamily, fontUrl) => {if (!fontUrl) {return;}const prevStyle = document.getElementById(fontFamily);if (prevStyle) {return;}const style = document.createElement('style');style.innerHTML = `@font-face {font-family: '${fontFamily}';src: url(${fontUrl});}`;// .replace(/\s+/g, ''); 去掉不然有空格的字体名称匹配不上style.id = fontFamily;document.head.appendChild(style);
};
http://www.lryc.cn/news/114579.html

相关文章:

  • laravel安装composer依赖
  • 问题聚集度Hive SQL
  • Windows11右键菜单
  • 篇十四:观察者模式:对象间的通知与更新
  • Hadoop知识点总结
  • 相关搜索量激增10000%!“芭比周边”产品火爆亚马逊!
  • C高级第四讲
  • Idea小操作
  • 【计算机网络】socket编程
  • 2023华为OD机试真题 Python 实现【寻找最大价值的矿堆/深度优先搜索】
  • 【Java面试】Nacos自动注册原理实现以及服务注册更新并如何保存到注册表
  • linux 手动编译安装 pkg-config 步骤
  • 【MongoDB】数据库、集合、文档常用CRUD命令
  • 【JVM】是如何管理内存的
  • 进程与线程、线程创建、线程周期、多线程安全和线程池(ThreadPoolExecutor)
  • 《论文阅读13》Efficient Urban-scale Point Clouds Segmentationwith BEV Projection
  • Django实现音乐网站 ⑻
  • VScode中同时打开两个脚本
  • 能源电力工程师专属Python学习资料
  • 推荐5款实用软件,提高工作效率,丰富生活乐趣
  • Python爬虫在电商数据挖掘中的应用
  • element-ui 表格el-table的列内容溢出省略显示,鼠标移上显示全部和定制样式
  • 研究人员发现特斯拉汽车能被越狱,可免费解锁付费功能
  • 【设计模式】责任链的基本概念及使用Predicate灵活构造校验链
  • Taro保存图片到手机
  • zookeeper --- 基础篇
  • Golang map 常用方法
  • 用于视觉跟踪的在线特征选择研究(Matlab代码实现)
  • Docker基本使用
  • PHP生成随机数