2011-01-28 67 views
2

我正在做一个脚本,旋转li在给定的ul。我想知道是否有可能打破我有一个李的徘徊时的递归。理想情况下,我会创建一个布尔值,不管递归是否应该继续,因为我希望将来有一个视频嵌入时可以让它断开。我刚开始,所以这基本上就是我所拥有的。Javascript/jQuery是否有可能通过另一个函数打破函数?

HTML:

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#ulRotator").rotate(); 
}); 
    </script> 
    </head> 
    <body> 
<ul id="ulRotator" style="width:500px; height:500px;"> 
    <li style="background-color:red;"></li> 
     <li style="background-color:blue;"></li> 
     <li style="background-color:black;"></li> 
     <li style="background-color:green;"></li> 
     <li style="background-color:grey;"></li> 
    </ul> 

</body> 

的Javascript:

(function($){ 

var rotator; 
var rotatorLi; 

$.fn.rotate = function() { 
    rotator = this; 
    rotatorLi = rotator.children('li'); 

    rotatorLi.css('width',rotator.css('width')).css('height',rotator.css('height')); 
    rotator.addClass('rotator'); 
    $(rotatorLi[0]).addClass('current'); 

    moveSlides('right'); 
}; 


moveSlides = function(direction){ 
    var current = $(rotator).find('li.current'); 
    var currentPosition = $(rotatorLi).index(current); 
    var slideCount = $(rotatorLi).length - 1; 

    var next; 
    if (direction == 'right'){ 
    if(currentPosition == slideCount){ 
    next = rotatorLi[0]; 
    }else{  
    next = rotatorLi[currentPosition+1]; 
    } 
    } 
    else if (direction == 'left'){ 
    if(currentPosition == 0){ 
    next = rotatorLi[slideCount]; 
    }else{  
    next = rotatorLi[currentPosition-1]; 
    } 
    } 

    $(current).delay(6000).fadeOut(500,function(){ 
    $(current).removeClass('current'); 
    $(next).addClass('current'); 
    $(next).css('display','block'); 
    moveSlides(direction); 
    }); 
}; 
    })(jQuery); 

CSS

.rotator li 
    { 
position:absolute; 
z-index:0; 
display::block !important; 
list-style-type:none; 
    } 
    li.current 
    { 
z-index:1 !important; 
    } 

另外请注意,我认为自己是一个巨大的新手,当涉及到的Javascript,我可能会去各地这在一个非常愚蠢的方式没有我知道它,任何指针将不胜感激。干杯。

回答

1

我使用mouseover中的公共函数将变量abort设置为true,并在moveSlides的第一行检查它。如果它被设置为true,那么只需return即可。

+0

请原谅我的无知,因此如果函数在循环函数运行时被调用,那么它不必等待? 我假设当停止另一个的功能也可以在它完成时再次启动它?假设使用悬停? – 2011-01-28 20:38:39

相关问题