預加載圖檔是提高使用者體驗的一個很好方法。圖檔預先加載到浏覽器中,通路者便可順利地在你的網站上沖浪,并享受到極快的加載速度。
這對圖檔畫廊及圖檔占據很大比例的網站來說十分有利,它保證了圖檔快速、無縫地釋出,也可幫助使用者在浏覽你網站内容時獲得更好的使用者體驗。
本文将分享三個不同的預加載技術,來增強網站的性能與可用性。
方法一:用css和JavaScript實作預加載
實作預加載圖檔有很多方法,包括使用css、JavaScript及兩者的各種組合。這些技術可根據不同設計場景設計出相應的解決方案,十分高效。
單純使用CSS,可容易、高效地預加載圖檔,代碼如下:
#preload-01 { background: url(http://domain.tld/image-01.png) no-repeat -9999px -9999px; }
#preload-02 { background: url(http://domain.tld/image-02.png) no-repeat -9999px -9999px; }
#preload-03 { background: url(http://domain.tld/image-03.png) no-repeat -9999px -9999px; }
将這三個ID選擇器應用到(X)html元素中,我們便可通過CSS的background屬性将圖檔預加載到螢幕外的背景上。
隻要這些圖檔的路徑保持不變,當它們在Web頁面的其他地方被調用時,浏覽器就會在渲染過程中使用預加載(緩存)的圖檔。簡單、高效,不需要任何JavaScript。
該方法雖然高效,但仍有改進餘地。使用該法加載的圖檔會同頁面的其他内容一起加載,增加了頁面的整體加載時間。
為了解決這個問題,我們增加了一些JavaScript代碼,來推遲預加載的時間,直到頁面加載完畢。代碼如下:
function preloader() {
if (document.getElementById) {
document.getElementById("preload-01").style.background = "url(http://domain.tld/image-01.png) no-repeat -9999px -9999px";
document.getElementById("preload-02").style.background = "url(http://domain.tld/image-02.png) no-repeat -9999px -9999px";
document.getElementById("preload-03").style.background = "url(http://domain.tld/image-03.png) no-repeat -9999px -9999px";
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(preloader);
在該腳本的第一部分,我們擷取使用類選擇器的元素,并為其設定了background屬性,以預加載不同的圖檔。
該腳本的第二部分,我們使用addLoadEvent()函數來延遲preloader()函數的加載時間,直到頁面加載完畢。
如果JavaScript無法在使用者的浏覽器中正常運作,會發生什麼?很簡單,圖檔不會被預加載,當頁面調用圖檔時,正常顯示即可。
方法二:僅使用JavaScript實作預加載
上述方法有時确實很高效,但我們逐漸發現它在實際實作過程中會耗費太多時間。相反,我更喜歡使用純JavaScript來實作圖檔的預加載。
下面将提供兩種這樣的預加載方法,它們可以很漂亮地工作于所有現代浏覽器之上。
JavaScript代碼段1
隻需簡單編輯、加載所需要圖檔的路徑與名稱即可,很容易實作:
<div class="hidden">
<script type="text/javascript">
var images = new Array()
function preload() {
for (i = 0; i < preload.arguments.length; i++) {
images[i] = new Image()
images[i].src = preload.arguments[i]
}
}
preload(
"http://domain.tld/gallery/image-001.jpg",
"http://domain.tld/gallery/image-002.jpg",
"http://domain.tld/gallery/image-003.jpg"
)
</script>
</div>
該方法尤其适用預加載大量的圖檔。我的畫廊網站使用該技術,預加載圖檔數量達50多張。将該腳本應用到登入頁面,隻要使用者輸入登入帳号,大部分畫廊圖檔将被預加載。
JavaScript代碼段2
該方法與上面的方法類似,也可以預加載任意數量的圖檔。将下面的腳本添加入任何Web頁中,根據程式指令進行編輯即可。
<div class="hidden">
<script type="text/javascript">
if (document.images) {
img1 = new Image();
img2 = new Image();
img3 = new Image();
img1.src = "http://domain.tld/path/to/image-001.gif";
img2.src = "http://domain.tld/path/to/image-002.gif";
img3.src = "http://domain.tld/path/to/image-003.gif";
}
</script>
</div>
正如所看見,每加載一個圖檔都需要建立一個變量,如“img1 = new Image();”,及圖檔源位址聲明,如“img3.src = “../path/to/image-003.gif”;”。參考該模式,你可根據需要加載任意多的圖檔。
我們又對該方法進行了改進。将該腳本封裝入一個函數中,并使用 addLoadEvent(),延遲預加載時間,直到頁面加載完畢。
function preloader() {
if (document.images) {
var img1 = new Image();
var img2 = new Image();
var img3 = new Image();
img1.src = "http://domain.tld/path/to/image-001.gif";
img2.src = "http://domain.tld/path/to/image-002.gif";
img3.src = "http://domain.tld/path/to/image-003.gif";
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(preloader);
方法三:使用Ajax實作預加載
上面所給出的方法似乎不夠酷,那現在來看一個使用Ajax實作圖檔預加載的方法。該方法利用DOM,不僅僅預加載圖檔,還會預加載CSS、JavaScript等相關的東西。使用Ajax,比直接使用JavaScript,優越之處在于JavaScript和CSS的加載不會影響到目前頁面。該方法簡潔、高效。
window.onload = function() {
setTimeout(function() {
// XHR to request a js and a CSS
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.js');
xhr.send('');
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.tld/preload.css');
xhr.send('');
// preload image
new Image().src = "http://domain.tld/preload.png";
}, 1000);
};
上面代碼預加載了“preload.js”、“preload.css”和“preload.png”。1000毫秒的逾時是為了防止腳本挂起,而導緻正常頁面出現功能問題。
下面,我們看看如何用JavaScript來實作該加載過程:
window.onload = function() {
setTimeout(function() {
// reference to <head>
var head = document.getElementsByTagName('head')[0];
// a new CSS
var css = document.createElement('link');
css.type = "text/css";
css.rel = "stylesheet";
css.href = "http://domain.tld/preload.css";
// a new JS
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "http://domain.tld/preload.js";
// preload JS and CSS
head.appendChild(css);
head.appendChild(js);
// preload image
new Image().src = "http://domain.tld/preload.png";
}, 1000);
};
這裡,我們通過DOM建立三個元素來實作三個檔案的預加載。正如上面提到的那樣,使用Ajax,加載檔案不會應用到加載頁面上。
從這點上看,Ajax方法優越于JavaScript。