jQuery 图像预加载在 FireFox 中第一次不起作用

发布于 2024-10-07 02:32:28 字数 738 浏览 2 评论 0原文

我刚刚完成了一个简单的 jQuery 画廊与一些淡入淡出过渡的组合,如此处所示。在所有浏览器中一切都工作正常 - 除了“图像预加载”在 FireFox 中的第一次加载时不起作用(在所有其他浏览器中工作)。图像在 Firefox 中保持 0% 不透明度。不知道为什么。

这是预加载代码。

$(document).ready(function(){
    //--------PRELOAD LOAD IMAGES--------\\
    $('img').load(function() {
        //once image has loaded fade in image
        $(this).animate({opacity : 1.0}, 1000);

        //kill padding on last thumbnail of each line
        $('#headshots img').eq(3).css({'padding-right' : '0'});
        $('#ports img').eq(3).css({'padding-right' : '0'});
        $('#ports img').eq(7).css({'padding-right' : '0'});
    });
});

预先感谢您的任何帮助。

I just finished putting together a simple jQuery gallery with some fading transitions as seen here. Everything works fine in all browsers - except that the "image preloading" doesn't work on the first load in FireFox (works in all other browsers). The images stay at 0% opacity in firefox. Not sure why.

Here is the preload code.

$(document).ready(function(){
    //--------PRELOAD LOAD IMAGES--------\\
    $('img').load(function() {
        //once image has loaded fade in image
        $(this).animate({opacity : 1.0}, 1000);

        //kill padding on last thumbnail of each line
        $('#headshots img').eq(3).css({'padding-right' : '0'});
        $('#ports img').eq(3).css({'padding-right' : '0'});
        $('#ports img').eq(7).css({'padding-right' : '0'});
    });
});

Thanks in advance for any help.

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

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

发布评论

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

评论(2

合约呢 2024-10-14 02:32:28

出于好奇,请尝试:

$(this).each(function() {
    $(this).animate({opacity : 1.0}, 1000);
});

为了使您的解决方案更加健壮,您应该考虑在浏览器缓存了每个图像的情况下强制触发加载事件,这可以防止触发加载事件。您可以通过测试 .complete 属性来做到这一点:

$('img').load(function() {
    $(this).each(function() {
        $(this).animate({opacity : 1.0}, 1000);
    });
    $('#headshots img').eq(3).css({'padding-right' : '0'});
    $('#ports img').eq(3).css({'padding-right' : '0'});
    $('#ports img').eq(7).css({'padding-right' : '0'});
}).each(function() {
    if(this.complete) $(this).trigger("load");
});

As a matter of curiosity, try:

$(this).each(function() {
    $(this).animate({opacity : 1.0}, 1000);
});

To make your solution more robust, you should consider forcing the load event to fire for each image in the event that it has been cached by the browser, which can prevent it's load event from triggering. You can do this by testing for the .complete property:

$('img').load(function() {
    $(this).each(function() {
        $(this).animate({opacity : 1.0}, 1000);
    });
    $('#headshots img').eq(3).css({'padding-right' : '0'});
    $('#ports img').eq(3).css({'padding-right' : '0'});
    $('#ports img').eq(7).css({'padding-right' : '0'});
}).each(function() {
    if(this.complete) $(this).trigger("load");
});
雾里花 2024-10-14 02:32:28

我对你的“firefox首次加载修复”有一些疑问。我使用稍微改变的代码来消除上述与 jquery flexslider 相关的 Firefox 错误。

$(window).load(function() { 
  $('.flexslider').flexslider({
        animation: "fade",              //String: Select your animation type, "fade" or "slide"
        slideDirection: "horizontal",   //String: Select the sliding direction, "horizontal" or "vertical"
        slideshow: true,                //Boolean: Animate slider automatically
        slideshowSpeed: 7000,           //Integer: Set the speed of the slideshow cycling, in milliseconds
        animationDuration: 1500,         //Integer: Set the speed of animations, in milliseconds
        directionNav: false,            //Boolean: Create navigation for previous/next navigation? (true/false)
        controlNav: false,              //Boolean: Create navigation for paging control of each clide? Note: Leave true for manualControls usage
        controlsContainer: ".flex-container"
  });
});

$(document).ready(function(){
    $('img').load(function() {
        $(this).each(function() {
            /*alert("each func");*/
            /*$(this).animate({opacity : 1.0}, 1000);*/
        });
    }).each(function() {
        if(this.complete) {
            //var src = $(this).attr("src");
            //alert(src);
            $(this).trigger("load");
        };
    });
});
  1. 我把它放在 flexslider 设置之后。这是正确/好的吗?
  2. 我不明白它是如何工作的。您解释说,如果图像来自浏览器缓存,则加载事件将不会触发。但后来我想知道“$('img').load(function()”是否或为什么被触发。我的意思是,当由于图像来自缓存而未触发事件时,我们无法挂钩“img” .load 因为我不会被解雇。
    换句话说:请解释当图像来自缓存时会发生什么。代码或事件“区域”中的哪些内容失败了?

更新:www.ozeankreuzer.de 每天第一次加载时仍然失败。怎么了?
错误是: document.body 未定义: https://dl.dropbox.com/u/31225678/Screenshot%20-%2014.06.2012%20%2C%2003_12_28.png

I have some question about your "firefox firstload fix". I use slightly changed code to eliminate the above described firefox error in connection with the jquery flexslider.

$(window).load(function() { 
  $('.flexslider').flexslider({
        animation: "fade",              //String: Select your animation type, "fade" or "slide"
        slideDirection: "horizontal",   //String: Select the sliding direction, "horizontal" or "vertical"
        slideshow: true,                //Boolean: Animate slider automatically
        slideshowSpeed: 7000,           //Integer: Set the speed of the slideshow cycling, in milliseconds
        animationDuration: 1500,         //Integer: Set the speed of animations, in milliseconds
        directionNav: false,            //Boolean: Create navigation for previous/next navigation? (true/false)
        controlNav: false,              //Boolean: Create navigation for paging control of each clide? Note: Leave true for manualControls usage
        controlsContainer: ".flex-container"
  });
});

$(document).ready(function(){
    $('img').load(function() {
        $(this).each(function() {
            /*alert("each func");*/
            /*$(this).animate({opacity : 1.0}, 1000);*/
        });
    }).each(function() {
        if(this.complete) {
            //var src = $(this).attr("src");
            //alert(src);
            $(this).trigger("load");
        };
    });
});
  1. I put it after the setup of the flexslider. Is this correct/good like that?
  2. I dont't get how it works. You explain that if an image comes frome the browser-cache the load event will not fire. But then I wounder if or why "$('img').load(function()" gets fired. I mean, when the event is not fired because the image came from the cache, we can't hook the 'img'.load because i won't be fired.
    Other words: Please explain what happens when a image comes from the cache. What in the code or event "area" fails?

UPDATE: www.ozeankreuzer.de it still fails on first load each day. What's wrong?
error is: document.body is undefined: https://dl.dropbox.com/u/31225678/Screenshot%20-%2014.06.2012%20%2C%2003_12_28.png

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