jQuery对图像加载的回调(即使在缓存映像时也是如此)我想做的是:$("img").bind('load', function() {
// do stuff
});但是,当从缓存加载图像时,LOAD事件不会触发。jQuery文档建议插件来解决这个问题,但是不管用
3 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
$(document).ready(function() {
var tmpImg = new Image() ;
tmpImg.src = $('#img').attr('src') ;
tmpImg.onload = function() {
// Run onload code.
} ;
}) ;$(document).ready(function() {
var imageLoaded = function() {
// Run onload code.
}
$('#img').each(function() {
var tmpImg = new Image() ;
tmpImg.onload = imageLoaded ;
tmpImg.src = $(this).attr('src') ;
}) ;
}) ;
慕的地6264312
TA贡献1817条经验 获得超6个赞
/**
* Trigger a callback when the selected images are loaded:
* @param {String} selector
* @param {Function} callback
*/
var onImgLoad = function(selector, callback){
$(selector).each(function(){
if (this.complete || /*for IE 10-*/ $(this).height() > 0) {
callback.apply(this);
}
else {
$(this).on('load', function(){
callback.apply(this);
});
}
});
};onImgLoad('img', function(){
// do stuff
});$('img').hide();
onImgLoad('img', function(){
$(this).fadeIn(700);
});/**
* Trigger a callback when 'this' image is loaded:
* @param {Function} callback
*/
(function($){
$.fn.imgLoad = function(callback) {
return this.each(function() {
if (callback) {
if (this.complete || /*for IE 10-*/ $(this).height() > 0) {
callback.apply(this);
}
else {
$(this).on('load', function(){
callback.apply(this);
});
}
}
});
};
})(jQuery);$('img').imgLoad(function(){
// do stuff
});$('img').hide().imgLoad(function(){
$(this).fadeIn(700);
});添加回答
举报
0/150
提交
取消
