创建一个小的jQuery实用程序方法:
jQuery.preloadImages = function() {
var set = jQuery('img');
var current = 0;
var iterate = function() {
var current_src = set[current].src;
var temp = jQuery('<img/>');
jQuery(temp).bind('load', function() {
console.log($(this).attr('src') + ' loaded');
jQuery(this).remove(); //remove temp image from DOM.
});
temp[0].src = current_src;
jQuery('body').append(temp);
if(++current < set.length) iterate(); //recursive call
};
iterate();
};
Run Code Online (Sandbox Code Playgroud)
像这样调用它:
$.preloadImages();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1955 次 |
| 最近记录: |