Jquery:DOM 加载后淡入图像。有时有效..?

发布于 2024-10-04 14:23:42 字数 827 浏览 7 评论 0原文

 //fade in images after the image has loaded..

$(document).ready(function(){
  $(".image_ad").hide().bind("load", function () { $(this).fadeIn(400); });
 });

如果有人有任何意见,那就太好了。

我写这个是为了避免必须观看页面上加载的图像,我宁愿页面加载,然后当每个图像准备好时它们会很好地淡入。

问题是,有时一些图像永远不会加载,点击刷新会纠正这个问题,但我宁愿让它完美,并询问是否有人知道为什么。

我有一种感觉,有时脚本运行时 dom 还没有完全加载,我知道它在文档中。准备好了,但这可能是可能的..

再次感谢。


感谢您的所有回复!今晚我将使用这些片段并将我发现有用的内容发回。再次非常感谢您的回答。


如下所示,这似乎非常适合我的需求。谢谢大家。

$(document).ready(function(){
   $(".gallery_image").hide().not(function() {
       return this.complete && $(this).fadeIn(100);
   }).bind("load", function () { $(this).fadeIn(100); });
});

还有一件事,我不喜欢不完整的帖子,所以...

我发现这在 Firefox 3.6.12 中不起作用。

我会调查一下。

 //fade in images after the image has loaded..

$(document).ready(function(){
  $(".image_ad").hide().bind("load", function () { $(this).fadeIn(400); });
 });

If anyone has any input this would be great.

I wrote this to avoid having to watch images load on the page, I rather the page loaded then when each image is ready they fade in nicely.

The problem is that sometimes a couple of images will never load, hitting refresh will correct this but I would rather get it perfect and ask if anyone has any idea why.

I have a feeling that sometimes the dom hasn't fully loaded by the time the script has run, I know it's in a document.ready but it might be possible..

thanks again.


Thanks for all the replies! I'll play with the snippets tonight and post back which I found useful. Again your answers are much appreciated.


As seen below, this seems to work very well for my needs. Thanks everyone.

$(document).ready(function(){
   $(".gallery_image").hide().not(function() {
       return this.complete && $(this).fadeIn(100);
   }).bind("load", function () { $(this).fadeIn(100); });
});

just one more thing, I dont like incompleted posts so...

I found that this doesnt work in Firefox 3.6.12.

I'll look in to this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深爱不及久伴 2024-10-11 14:23:42

我猜这是因为图像被缓存了,所以 load 永远不会触发。

尝试排除隐藏的缓存图像:

$(document).ready(function(){
   $(".image_ad").not(function() {
       return this.complete;
   }).hide().bind("load", function () { $(this).fadeIn(400); });
});

或隐藏它们,但立即对完整图像执行fadeIn

$(document).ready(function(){
   $(".image_ad").hide().not(function() {
       return this.complete && $(this).fadeIn(400);
   }).bind("load", function () { $(this).fadeIn(400); });
});

I'm guessing it is because the images are cached, so the load never fires.

Try excluding cached images from being hidden:

$(document).ready(function(){
   $(".image_ad").not(function() {
       return this.complete;
   }).hide().bind("load", function () { $(this).fadeIn(400); });
});

or hide them, but do a fadeIn on the complete ones immediately.

$(document).ready(function(){
   $(".image_ad").hide().not(function() {
       return this.complete && $(this).fadeIn(400);
   }).bind("load", function () { $(this).fadeIn(400); });
});
冰之心 2024-10-11 14:23:42

我有一种感觉,有时
此时 dom 尚未完全加载
脚本已经运行,我知道它在
document.ready 但可能是
可能..

我认为问题恰恰相反:dom 可能在图像加载后完全加载。因此,当您的加载处理程序被附加时,图像将已经加载并且处理程序永远不会被触发。

您可以通过检查 image.complete 或通过 js 插入图像并立即附加加载处理程序来解决此问题。

尝试:(

$(document).ready(function(){
    $(".image_ad").hide().bind("load", function () { $(this).fadeIn(400); }).each(function() {
        if(this.complete && jQuery(this).is(":hidden")) {
            jQuery(this).fadeIn(400);
        }
    });
});

或类似的东西)。

I have a feeling that sometimes the
dom hasn't fully loaded by the time
the script has run, I know it's in a
document.ready but it might be
possible..

I think the problem is rather the other way around: the dom may have fully loaded after the images have. So when your load handler gets attached, the images will already have loaded and the handler is never fired.

You can work around this either by checking for image.complete or by inserting the images via js and attaching the load handler right away.

Try:

$(document).ready(function(){
    $(".image_ad").hide().bind("load", function () { $(this).fadeIn(400); }).each(function() {
        if(this.complete && jQuery(this).is(":hidden")) {
            jQuery(this).fadeIn(400);
        }
    });
});

(or something like that).

睡美人的小仙女 2024-10-11 14:23:42

不确定这是否有帮助,但是您可以通过将其添加到 css 中来摆脱 hide() 调用:

img.image_ad{
  display: none;
}

Not sure if this will help or not, but you can get rid of the hide() call by just adding this to your css:

img.image_ad{
  display: none;
}
尾戒 2024-10-11 14:23:42

您最好通过 css 类隐藏图像

.image_ad { display:none; }

然后设置:

$(document).ready(function(){
    $('.image_ad').fadeIn(400);
});

这将确保图像一开始就隐藏,然后一旦接收并加载所有内容,就将图像淡入。

You are better off hiding the images via css class

.image_ad { display:none; }

And then set:

$(document).ready(function(){
    $('.image_ad').fadeIn(400);
});

What this will do is make sure the images are hidden to begin with, and then once everything is received and loaded, fade the images in.

忆伤 2024-10-11 14:23:42

我根据在另一个论坛上找到的一些代码改编了这个非常简单的功能。它包含一个回调函数,供您在图像加载完成时使用。虽然它不会在加载每个图像时淡出,但当指定容器中的所有图像完成加载时,它会在所有图像中淡出。我确信您可以根据需要调整此代码。

function imgsLoaded(el,f){
  var imgs = el.find('img');
  var num_imgs = imgs.length;

  if(num_imgs > 0){
    imgs.load(function(){
      num_imgs--;
      if(num_imgs == 0) {
        if(typeof f == "function")f();//custom callback
      }
    });
  }else{
    if(typeof f == "function")f();//custom callback
  }
}

用法:

imgsLoaded($('#container'),function(){

$('.image_ad').fadeIn(400);

});

希望这有帮助!

W.

I adapted this very simple function from some code I found on another forum. It includes a callback function for you to use when the images have finished loading. Although it doesn't fade in each image as it has loaded, it fades in all the images when all the images in a stated container have finished loading. I'm sure you can adapt this code it need be.

function imgsLoaded(el,f){
  var imgs = el.find('img');
  var num_imgs = imgs.length;

  if(num_imgs > 0){
    imgs.load(function(){
      num_imgs--;
      if(num_imgs == 0) {
        if(typeof f == "function")f();//custom callback
      }
    });
  }else{
    if(typeof f == "function")f();//custom callback
  }
}

USAGE:

imgsLoaded($('#container'),function(){

$('.image_ad').fadeIn(400);

});

Hope this helps!

W.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文