【问题标题】:Preloading Image Bug in IE6-8在 IE6-8 中预加载图像错误
【发布时间】: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


【解决方案1】:

我看到你已经解决了这个问题,但我想看看我是否可以让预加载也能在 IE 中工作。

尝试改变这个

photo.fadeOut('slow', function() { 
  photo.attr('src', img_src); 
  photo.load(function() { 
    photoWrapper.removeClass('loading'); 
    photo.fadeIn('slow'); 
  }); 
}); 

到这里

photo.fadeOut('slow', function() { 
  photo.attr('src', img_src); 

  if (photo[0].complete){
    photoWrapper.removeClass('loading'); 
    photo.fadeIn('slow'); 
  } else {  
    photo.load(function() { 
      photoWrapper.removeClass('loading'); 
      photo.fadeIn('slow'); 
    }); 
  }
});

【讨论】:

  • 很抱歉,我花了这么长时间才知道这个,太忙了。你的方法奏效了!在大多数情况下......唯一的问题是,每一次都有某种奇怪的闪烁......但我无法确定它,它很难复制。哦,好吧,无论如何我现在都在使用你的新代码,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2011-06-26
  • 2014-06-23
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
相关资源
最近更新 更多