如何訪問DOM以檢測我的頁面的Web字體何時加載?如何檢測base-64編碼的網頁字體的「onload」?
我有29個.ttf字體base64編碼成一個CSS文件。我也在使用每種字體的頁面上隱藏了div
,以使瀏覽器將每種字體讀入內存。該過程需要時間,所以我需要在瀏覽器將每種字體加載到內存後排隊AJAX事件。
*我知道document.ready
和window.onload
會關閉,但我想在外部資源的其餘部分完成加載之前觸發事件,以使AJAX更快啓動。
(jQuery的作爲最後一招,請)
================
加樣品代碼:
HTML
<head>
...
<link type="text/css" rel="stylesheet" href="TTF-embedded-fonts.css" />
<script type="text/javascript" src="load-fonts.js"></script>
</head>
負載-FO nts.js
var ST1 = document.createElement('div');
ST1.setAttribute('id', 'fontloader');
ST1.innerHTML = "<span style='font-family:samplefont'>a<b>b<i>c</i></b><i>d</i></span> ... ";
ST1.style.cssText = "height:0px;text-indent:-9999px;visibility:hidden";
window.document.body.onload = appendST1();
function appendST1() { document.body.appendChild(ST1) };
[FONT-LOAD HANDLER HERE] { API.Ajax.loadComplete("load-fonts.js") };
TTF-嵌入式fonts.css
@font-face {
font-family: 'samplefont';
src: url(data:font/truetype;charset=utf-8;base64,AAAE...) format('truetype');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'samplefont';
src: url(data:font/truetype;charset=utf-8;base64,AAAE...) format('truetype');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'samplefont';
src: url(data:font/truetype;charset=utf-8;base64,AAAE...) format('truetype');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'samplefont';
src: url(data:font/truetype;charset=utf-8;base64,AAAE...) format('truetype');
font-weight: bold;
font-style: italic;
}
舉例說明了你的意思? –
@詹姆斯 - 我發佈了一份我正在使用的內容。 –