jQuery setInterval 与 chrome 中的 fadeIn、fadeOut 错误?

发布于 2024-10-28 20:05:38 字数 1883 浏览 7 评论 0原文

我要疯狂地想弄清楚这个问题。在我网站的主页上,我有一个滑块,当前有四个项目(html 是动态创建的,因此该解决方案必须适用于任意数量的项目)。这些项目绝对位于彼此之上。该脚本可以执行两件事:

1)当单击滑块导航项(这是一个锚链接)时,当前导航项淡出,新导航项淡入(交叉淡入淡出)。

2) 如果用户没有单击任何导航项,滑块将自动前进到下一个项。

当我尝试使用 setInterval 时,我的问题仅出现在 Chrome 中。当我这样做时,交叉淡入淡出不起作用,传出的容器淡出为白色,然后传入的容器突然出现(没有褪色)。

这是我的代码。希望有一些想法,因为我已经尝试了一切!

jQuery(document).ready(function() {

    jQuery('.slider-item:first').show();
    jQuery('.slider-nav-item').click(function () { 


        if (!jQuery(this).hasClass("activeSlide")) {


            clearInterval(s);
            s = setInterval ( "nextSlide()", 6000 );

            currentSlider = jQuery(this).attr("rel");

            jQuery('#slider .slider-item').fadeOut(1200);
            jQuery('#slider .slider-item:nth-child('+ currentSlider+')').fadeIn(1200);

            jQuery('.slider-nav-item').removeClass("activeSlide"); 
            jQuery(this).addClass("activeSlide");

        }

        return false;
     });

    var s = setInterval ( "nextSlide()", 6000 );


});


function nextSlide (){

    var max = jQuery('#slider-nav .slider-nav-item').length;

    var currentSlider = jQuery('.activeSlide').attr("rel");

    if (currentSlider == max) {

        jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
        jQuery('#slider .slider-item:first').fadeIn(1200);

        jQuery('.slider-nav-item').removeClass("activeSlide"); 
        jQuery('.slider-nav-item:first').addClass("activeSlide");

    } else {

        var temp = parseInt(currentSlider) + 1;

        jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
        jQuery('#slider .slider-item:nth-child('+temp+')').fadeIn(1200);

        jQuery('.slider-nav-item').removeClass("activeSlide"); 
        jQuery('#slider-nav .slider-nav-item:nth-child('+temp+')').addClass("activeSlide");

    }

}

I'm going crazy trying to figure this out. On the homepage of my site I have a slider that currently has four items (the html is created dynamically, so this solution has to work for any number of items). These items are positioned absolutely on top of each other. The script can do two things:

1) When clicking on a slider-nav-item (which is a anchor link), the current nav item fades out, and the new one fades in (cross-fades).

2) If the user does not click on any nav items, the slider will automatically progress to the next item.

My problem is only occurring in Chrome when I try to use setInterval. When I do this, the cross fading does not work, and the outgoing container fades out to white, and then the container coming in just abruptly appears (no fading).

Here is my code. Hoping for some ideas, because I've tried everything!

jQuery(document).ready(function() {

    jQuery('.slider-item:first').show();
    jQuery('.slider-nav-item').click(function () { 


        if (!jQuery(this).hasClass("activeSlide")) {


            clearInterval(s);
            s = setInterval ( "nextSlide()", 6000 );

            currentSlider = jQuery(this).attr("rel");

            jQuery('#slider .slider-item').fadeOut(1200);
            jQuery('#slider .slider-item:nth-child('+ currentSlider+')').fadeIn(1200);

            jQuery('.slider-nav-item').removeClass("activeSlide"); 
            jQuery(this).addClass("activeSlide");

        }

        return false;
     });

    var s = setInterval ( "nextSlide()", 6000 );


});


function nextSlide (){

    var max = jQuery('#slider-nav .slider-nav-item').length;

    var currentSlider = jQuery('.activeSlide').attr("rel");

    if (currentSlider == max) {

        jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
        jQuery('#slider .slider-item:first').fadeIn(1200);

        jQuery('.slider-nav-item').removeClass("activeSlide"); 
        jQuery('.slider-nav-item:first').addClass("activeSlide");

    } else {

        var temp = parseInt(currentSlider) + 1;

        jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
        jQuery('#slider .slider-item:nth-child('+temp+')').fadeIn(1200);

        jQuery('.slider-nav-item').removeClass("activeSlide"); 
        jQuery('#slider-nav .slider-nav-item:nth-child('+temp+')').addClass("activeSlide");

    }

}

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

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

发布评论

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

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-11-04 20:05:38

这不会解决问题(也许?)

var s = setInterval ( "nextSlide()", 6000 ); 

应该是(最佳实践)

setInterval ( function(){
  nextSlide();
}, 6000 ); 

另外为什么不只使用插件呢? http://css-tricks.com/3412-anythingslider-jquery-plugin/

This won't fix the problem (maybe?)

var s = setInterval ( "nextSlide()", 6000 ); 

should be (best practice)

setInterval ( function(){
  nextSlide();
}, 6000 ); 

Also why not just use a plugin? http://css-tricks.com/3412-anythingslider-jquery-plugin/

旧时浪漫 2024-11-04 20:05:38

我遇到了 jQuery 交叉淡入淡出问题,当 HTML 无效时,元素会突然完全按照您所描述的方式显示。通过验证器运行页面并更正所有语法问题,然后查看问题是否仍然存在。

我遇到过好几个这样的问题。 jQuery 似乎对有效的 HTML 有着强烈的偏好。

I have had issues with jQuery cross-fades where elements would abruptly appear exactly as you describe when the HTML wasn't valid. Run the page through a validator and correct any syntax issues, and see if the issue remains.

I've had several issues like this. jQuery seems to have a strong preference for valid HTML.

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