I'm using Font-Awesome, but while the font files are not loaded, the icons appear with .

所以,我希望这些图标有display:none个,而文件没有加载.

@font-face {
  font-family: "FontAwesome";
  src: url('../font/fontawesome-webfont.eot');
  src: url('../font/fontawesome-webfont.eot?#iefix') format('eot'), url('../font/fontawesome-webfont.woff') format('woff'), url('../font/fontawesome-webfont.ttf') format('truetype'), url('../font/fontawesome-webfont.svg#FontAwesome') format('svg');
  font-weight: normal;
  font-style: normal;
}

How do I know that these files have been loaded and I'm finally able to show the icons?

Edit:

推荐答案

现在在GitHub上:https://github.com/patrickmarabeas/jQuery-FontSpy.js

基本上,该方法通过比较两种不同字体的字符串宽度来工作.我们使用Comic Sans作为测试的字体,因为它是最不同的网络安全字体,希望与您将使用的任何自定义字体都有足够的不同.此外,我们使用了非常大的字体大小,所以即使是很小的差异也会很明显.计算完Comic Sans字符串的宽度后,字体系列将更改为自定义字体,并返回Comic Sans.选中时,如果字符串元素宽度相同,则Comic Sans的备用字体仍在使用.如果没有,你的字体应该是可操作的.

I rewrote the method of font load detection into a jQuery plugin designed to give the developer the ability to style elements based upon whether the font has been loaded or not. A fail safe timer has been added so the user isn’t left without content if the custom font fails to load. That’s just bad usability.

通过添加和删除类,我还增加了对字体加载和失败时发生的事情的更大控制.你现在可以对字体做任何你喜欢的事情.我只建议修改字体大小、行距等,使您的回退字体尽可能接近自定义字体,以便您的布局保持完整,用户获得预期的体验.

Here's a demo: http://patrickmarabeas.github.io/jQuery-FontSpy.js

把下面的句子放到一个句子里.js文件并引用它.

(function($) {

    $.fontSpy = function( element, conf ) {
        var $element = $(element);
        var defaults = {
            font: $element.css("font-family"),
            onLoad: '',
            onFail: '',
            testFont: 'Comic Sans MS',
            testString: 'QW@HhsXJ',
            delay: 50,
            timeOut: 2500
        };
        var config = $.extend( defaults, conf );
        var tester = document.createElement('span');
            tester.style.position = 'absolute';
            tester.style.top = '-9999px';
            tester.style.left = '-9999px';
            tester.style.visibility = 'hidden';
            tester.style.fontFamily = config.testFont;
            tester.style.fontSize = '250px';
            tester.innerHTML = config.testString;
        document.body.appendChild(tester);
        var fallbackFontWidth = tester.offsetWidth;
        tester.style.fontFamily = config.font + ',' + config.testFont;
        function checkFont() {
            var loadedFontWidth = tester.offsetWidth;
            if (fallbackFontWidth === loadedFontWidth){
                if(config.timeOut < 0) {
                    $element.removeClass(config.onLoad);
                    $element.addClass(config.onFail);
                    console.log('failure');
                }
                else {
                    $element.addClass(config.onLoad);
                    setTimeout(checkFont, config.delay);
                    config.timeOut = config.timeOut - config.delay;
                }
            }
            else {
                $element.removeClass(config.onLoad);
            }
        }
        checkFont();
    };

    $.fn.fontSpy = function(config) {
        return this.each(function() {
            if (undefined == $(this).data('fontSpy')) {
                var plugin = new $.fontSpy(this, config);
                $(this).data('fontSpy', plugin);
            }
        });
    };

})(jQuery);

Apply it to your project

.bannerTextChecked {
        font-family: "Lobster";
        /* don't specify fallback font here, do this in onFail class */
}

$(document).ready(function() {

    $('.bannerTextChecked').fontSpy({
        onLoad: 'hideMe',
        onFail: 'fontFail anotherClass'
    });

});

Remove that FOUC!

.hideMe {
    visibility: hidden !important;
}

.fontFail {
    visibility: visible !important;
    /* fall back font */
    /* necessary styling so fallback font doesn't break your layout */
}

编辑:删除了兼容性,因为它不能正常工作,并且在不同版本中遇到了问题.这里可以找到一个黑客补丁:https://github.com/patrickmarabeas/jQuery-FontFaceSpy.js/issues/1

Jquery相关问答推荐

加入了两个 select 语句,但数据显示了两次而不是一次

如果选中了三个sibling ,则选中一个复选框

如何在外部JS文件中使用带有参数的laravel路由

JQuery:$.get 不是函数

javascript 捕获浏览器快捷方式 (ctrl+t/n/w)

jQuery非只读 Select 器

Javascript 仅打印 iframe 内容

如何使用给定的 url 判断图像是否存在?

jQuery .load() 调用不会在加载的 HTML 文件中执行 JavaScript

如何使用 jQuery 为文本对齐动态添加样式

jQuery.extend 和 jQuery.fn.extend 的区别?

点击时保持 Bootstrap 下拉菜单打开

使用 Typescript 时如何停止类型 JQuery 上不存在属性语法错误?

JQuery Ajax Post 导致 500 内部服务器错误

捕获在页面任意位置按下的 Enter 键

jQuery 和 jQuery Mobile 的区别?

如何让 twitter bootstrap 子菜单在左侧打开?

变量首字母大写

通过 AJAX MVC 下载 Excel 文件

Ajax 处理中的无效 JSON 原语