【问题标题】:When do web-fonts load and can you pre-load them?网络字体什么时候加载,你可以预加载它们吗?
【发布时间】:2012-10-26 07:57:30
【问题描述】:

我注意到在使用网络字体时,它们最初可能需要一秒钟才能出现;就像您创建一个下拉导航菜单一样,当您第一次将鼠标悬停在菜单上时,整个菜单将显示为背景颜色一秒钟,然后出现文本。

这并不理想,它让我相信加载 CSS 文件时并没有下载 webfonts,而是在您第一次在页面上查看它们时下载。

但另一方面,我已经在我的电脑上安装了字体,因此不需要下载它们,因此提出了一个问题,即他们为什么要这样做!?

这是我用来加载网络字体的 CSS:

@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto-Regular-webfont.eot');
    src: url('../fonts/Roboto-Regular-webfont.eot?#iefix') format('embedded-opentype'),
         url('../fonts/Roboto-Regular-webfont.woff') format('woff'),
         url('../fonts/Roboto-Regular-webfont.ttf') format('truetype'),
         url('../fonts/Roboto-Regular-webfont.svg#RobotoRegular') format('svg');
    font-weight: normal;
    font-style: normal;
}

@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto-Italic-webfont.eot');
    src: url('../fonts/Roboto-Italic-webfont.eot?#iefix') format('embedded-opentype'),
         url('../fonts/Roboto-Italic-webfont.woff') format('woff'),
         url('../fonts/Roboto-Italic-webfont.ttf') format('truetype'),
         url('../fonts/Roboto-Italic-webfont.svg#RobotoItalic') format('svg');
    font-weight: normal;
    font-style: italic;
}

@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto-Bold-webfont.eot');
    src: url('../fonts/Roboto-Bold-webfont.eot?#iefix') format('embedded-opentype'),
         url('../fonts/Roboto-Bold-webfont.woff') format('woff'),
         url('../fonts/Roboto-Bold-webfont.ttf') format('truetype'),
         url('../fonts/Roboto-Bold-webfont.svg#RobotoBold') format('svg');
    font-weight: bold;
    font-style: normal;
}

@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto-Light-webfont.eot');
    src: url('../fonts/Roboto-Light-webfont.eot?#iefix') format('embedded-opentype'),
         url('../fonts/Roboto-Light-webfont.woff') format('woff'),
         url('../fonts/Roboto-Light-webfont.ttf') format('truetype'),
         url('../fonts/Roboto-Light-webfont.svg#RobotoLight') format('svg');
    font-weight: 300;
    font-style: normal;
}

@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto-Medium-webfont.eot');
    src: url('../fonts/Roboto-Medium-webfont.eot?#iefix') format('embedded-opentype'),
         url('../fonts/Roboto-Medium-webfont.woff') format('woff'),
         url('../fonts/Roboto-Medium-webfont.ttf') format('truetype'),
         url('../fonts/Roboto-Medium-webfont.svg#RobotoMedium') format('svg');
    font-weight: 500;
    font-style: normal;
}

【问题讨论】:

  • 你能发布你用来加载字体的代码吗?
  • @BillyMoat 抱歉,现在发布。 :)
  • 使用src: local()。另见:stackoverflow.com/questions/12312323/…
  • 您的字体系列名称不应该是 Roboto、RobotoItalic 和 RobotoBold 之类的吗?在调用字体时,您是否还指定了诸如“Roboto”、Helvetica、Arial、sans-serif 之类的内容?
  • @BillyMoat 对于你的后一个问题,是的,我是。对于你以前的问题,不,这样做意味着当网络字体下载失败并且字体应该是粗体时,你会遇到问题。见:stackoverflow.com/questions/12562631/…

标签: css fonts webfonts


【解决方案1】:

什么时候下载网络字体?

Paul Irish 制作了一个简单的页面来测试:http://dl.getdropbox.com/u/39519/webfontsdemo/loadtest.html

它表明大多数浏览器在页面中使用字体而不是在 CSS 中声明时下载字体。我相信 IE 是个例外,但我现在没有运行它来测试。

下载使用而不是声明的原因是为了减少不必要的网络流量,例如如果字体已声明但未使用。

可以避免字体下载吗?

您说得对,如果字体已经安装,则不需要下载它们。正如上面@Patrick 所说,这可以使用local() 来完成。它在 CSS 中位于 url() 之前并采用字体名称(Mac OS X 上的 Safari 随后需要 PostScript 名称)。尝试对您的 CSS 进行以下更改:

@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto-Regular-webfont.eot');
    src: local(Roboto Regular), local(Roboto-Regular),
         url('../fonts/Roboto-Regular-webfont.eot?#iefix') format('embedded-opentype'),
         url('../fonts/Roboto-Regular-webfont.woff') format('woff'),
         url('../fonts/Roboto-Regular-webfont.ttf') format('truetype'),
         url('../fonts/Roboto-Regular-webfont.svg#RobotoRegular') format('svg');
    font-weight: normal;
    font-style: normal;
}

最后,为了减少字体下载时间,您可以确保执行以下操作:

  • 将 CSS 放在 JavaScript 之前
  • 为未来添加 Expires 标头 字体(以便浏览器缓存它们)
  • 压缩字体

这里有一个很好的处理字体显示延迟的总结:http://paulirish.com/2009/fighting-the-font-face-fout/

【讨论】:

  • 很好的建议。您能否详细说明一下或提供一个链接,说明为什么将 CSS 放在 JS 之前会提高性能?
  • 更普遍的原因是,未动态加载的外部脚本会阻止进一步的下载和页面渲染,不仅在加载时,而且在执行时,所以将它们放在底部通常推荐页面的。另一方面,通常首先需要 CSS 以避免短暂显示无样式的页面。特别是关于 webfonts,IE 在字体下载之前不会呈现页面,如果它们位于脚本标签下方:stevesouders.com/blog/2009/10/13/font-face-and-performance
猜你喜欢
  • 2012-09-22
  • 2012-10-14
  • 2018-07-07
  • 2016-03-13
  • 2021-01-19
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 2013-09-07
相关资源
最近更新 更多