【发布时间】:2010-04-15 23:52:56
【问题描述】:
有问题的页面:http://phwsinc.com/our-work/one-rincon-hill.asp
在 IE6-8 中,当您单击图库中最左侧的缩略图时,图像永远不会加载。如果您再次单击缩略图,它将加载。我正在使用 jQuery,这是为画廊提供动力的代码:
$(document).ready(function() {
// PROJECT PHOTO GALLERY
var thumbs = $('.thumbs li a');
var photoWrapper = $('div.photoWrapper');
if (thumbs.length) {
thumbs.click( function(){
photoWrapper.addClass('loading');
var img_src = $(this).attr('href');
// The two lines below are what cause the bug in IE. They make the gallery run much faster in other browsers, though.
var new_img = new Image();
new_img.src = img_src;
var photo = $('#photo');
photo.fadeOut('slow', function() {
photo.attr('src', img_src);
photo.load(function() {
photoWrapper.removeClass('loading');
photo.fadeIn('slow');
});
});
return false;
});
}
});
一位同事告诉我,他一直对 js Image() 对象有问题,并建议我在 div 集内附加一个 <img /> 元素,设置为 display:none;,但这对我来说有点乱 - -我喜欢使用 Image() 对象,它使事情变得干净整洁,没有添加不必要的 HTML 标记。
任何帮助将不胜感激。它仍然可以在没有图像预加载的情况下工作,所以如果所有其他方法都失败了,我只需将预加载包装在 if !($.browser.msie){ } 中并收工。
【问题讨论】:
-
如果我是你,我会单独为
#photo设置“加载”处理程序,而不是像那样在每次点击时都这样做。 -
我不太明白——你能提供一个代码示例吗?每次单击缩略图时不需要运行加载处理程序(因此正在加载新图像)?否则它只会在 $(document).ready() 上运行 1 次。
标签: javascript jquery internet-explorer preloading