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

相關問題