简体   繁体   English

如何最小化加载 Font Awesome 图标的延迟?

[英]How to minimize the delay in loading the Font Awesome icons?

I have linked the minified CSS of Font Awesome from Bootstrap CDN in a webpage.我已经在网页中从 Bootstrap CDN 链接了 Font Awesome 的缩小版 CSS。 The problem is that the icons appear later than the rest of the page content after a visible delay.问题是图标在可见延迟后出现的时间晚于页面内容的 rest。

What is the best way to get rid of this delay?摆脱这种延迟的最佳方法是什么? (FYI, I have already included this CSS file above every other CSS and JS link in the head.) (仅供参考,我已经将这个 CSS 文件包含在所有其他 CSS 和头部的 JS 链接之上。)

The minified css would not make much of a difference in load time.缩小的 css 不会对加载时间产生太大影响。 That font awesome css file references the paths to external font files that will load in after the page loads.该字体真棒 css 文件引用了将在页面加载后加载的外部字体文件的路径。 You can see the delay on font awesome's website: https://fortawesome.github.io/Font-Awesome/icons/你可以在 font awesome 的网站上看到延迟: https : //fortawesome.github.io/Font-Awesome/icons/

@font-face {
  font-family: 'FontAwesome';
  src: url('../fonts/fontawesome-webfont.eot?v=4.5.0');
  src: url('../fonts/fontawesome-webfont.eot?#iefix&v=4.5.0') format('embedded-opentype'), url('../fonts/fontawesome-webfont.woff2?v=4.5.0') format('woff2'), url('../fonts/fontawesome-webfont.woff?v=4.5.0') format('woff'), url('../fonts/fontawesome-webfont.ttf?v=4.5.0') format('truetype'), url('../fonts/fontawesome-webfont.svg?v=4.5.0#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}

You could base64 encode the fonts and include them right in your site's css.您可以对字体进行 base64 编码并将它们直接包含在您网站的 css 中。 This would substantially increase your site's css load time, but it would get rid of the flash.这将大大增加您网站的 css 加载时间,但它会摆脱 flash。 Although it might not work in all browsers and I would not recommend it.虽然它可能不适用于所有浏览器,但我不推荐它。

You could try hosting the font awesome css and fonts directly on your server.您可以尝试直接在您的服务器上托管字体真棒 css 和字体。 The CDN might be the cause of the latency. CDN 可能是造成延迟的原因。

I resolved the issue by self hosting the icons, this is described at https://fontawesome.com/docs/web/setup/host-yourself/webfonts我通过自行托管图标解决了这个问题,这在https://fontawesome.com/docs/web/setup/host-yourself/webfonts中有描述

Try to change url() to data-uri()尝试将 url() 更改为 data-uri()

LESS:较少的:

data-uri('../fonts/fontawesome-webfont.woff2?v=4.5.0') format('woff2'), 

http://lesscss.org/functions/#misc-functions-data-uri http://lesscss.org/functions/#misc-functions-data-uri

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM