javascript setInterval一遍又一遍地淡化相同的图像而不是下一个

时间:2010-08-09 17:50:15

标签: javascript jquery

(编辑)  它应该每5秒旋转3个图像之间的淡入淡出。但它只是每隔5秒就会逐渐淡化同一个,永远不会进入下一个图像。这是mote上下文,因为错误可能不是由初始帖子中的任何内容引起的:

                $(".paging").show();  

                $(".paging a:first").addClass("active"); 

                $(".image_reel img:first").addClass("active");




                var imageWidth = $(".window").width();
                var imageSum = $(".image_reel div").size();  
                var imageReelWidth = imageWidth * imageSum;



                //Adjust the image reel to its new size
                $(".image_reel").css({'width' : imageReelWidth});  


                //Paging  and Slider Function
                rotate = function(){


                    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
                    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

                    $(".paging a").removeClass('active'); //Remove all active class

                    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

                    //Slider Animation
                    $(".image_reel").animate({
                        left: -image_reelPosition
                    }, 500 );

                    $(".image_text").animate({
                        left: -image_reelPosition
                    }, 500 );
                }; 
                //Below is part not working right
                fadeImg = function(){

                    $("#group1 img").removeClass('active'); 
                    $next.addClass('active');               

                    $("#group1 img").animate({
                        opacity: 0
                    }, 500 );

                    $next.animate({
                        opacity: 1
                    }, 500 );



                };

                //Rotation  and Timing Event
                rotateSwitch = function(){
                    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
                        $active = $('.paging a.active').next(); //Move to the next paging
                        if ( $active.length === 0) { //If paging reaches the end...
                            $active = $('.paging a:first'); //go back to first
                        }
                        rotate(); //Trigger the paging and slider function
                    }, 15000); //Timer speed in milliseconds (7 seconds)

                    playFade = setInterval(function(){ 

                        $next = $(".image_reel img.active").next();
                        if($next.length === 0){
                            $next = $(".image_reel img:first");
                        } 
                        fadeImg();
                    }, 5000);
                };

                rotateSwitch(); //Run function on launch

                //On Hover
                $(".image_reel a").hover(function() {
                    clearInterval(play); //Stop the rotation
                }, function() {
                    rotateSwitch(); //Resume rotation timer
                }); 

                //On Click
                $(".paging a").click(function() {
                    $active = $(this); //Activate the clicked paging
                    //Reset Timer
                    clearInterval(play); //Stop the rotation
                    rotate(); //Trigger rotation immediately
                    rotateSwitch(); // Resume rotation timer
                    return false; //Prevent browser jump to link anchor
                });

标记:

<div class="image_reel"> 
    <div id="group1">
        <a href="#"><img src="images/reel_1.png" class="first" /></a>  
        <a href="#"><img src="images/reel_1b.png" class="second" /></a>
        <a href="#"><img src="images/reel_1c.png" class="third" /></a>

        <p class="first">  A</p>
        <p class="second">  B</p>
        <p class="third">C</p>

        <div class="slidernav">
            <a href="#first"> A</a> 
            <a href="#second"> B</a> 
            <a href="#third"> C</a> 
        </div>
    </div>
</div>
​

请注意,我知道用于创建淡入淡出效果的插件,但我现在正尝试手动执行此操作。感谢您的回复。

1 个答案:

答案 0 :(得分:0)

问题是:$next = $(".image_reel img.active").next();没有做你想做的事。正在做的是找到img.active,然后尝试找到它的下一个兄弟,因为它没有任何兄弟$next被设置为一个空的jQuery对象。

你想要的是这样的:

$next = $(".image_reel img.active").parent().next().find('img');