Cross-browser webfonts loaded event

大城市里の小女人 提交于 2019-12-13 04:28:48

问题


I am trying to measure the width of a specific character from a webfont. The active event provided by Google's Webfont Loader fires slightly too early and the measurement it made on the default font.

A stripped down version of the script that I am working with:

window.WebFontConfig = {
    google: {
        families: ['Anonymous Pro:400,400italic,700,700italic']
    },

    active: function () {
        $.fn.ready(MeasureM);
    }
};

function MeasureM() {
    var e = document.getElementById('div');
    e.style.font = "15px/15px 'Anonymous Pro'";
    e.appendChild(document.createTextNode('M'));
    console.log(e.offsetWidth, e.offsetHeight);
}

(function () {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
        '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
})();

The HTML element:

<div id="div" style="display: table;"></div>

For convenience a jsFiddle.

The issue that I am having appear to only happen in Firefox, is there a solution? Or, is there something that I am misunderstanding?

来源:https://stackoverflow.com/questions/15469615/cross-browser-webfonts-loaded-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!