簡體   English   中英

Javascript / jQuery是否可以通過另一個函數來突破一個函數?

[英]Javascript/jQuery Is it possible to break out of a function through another function?

我正在做一個腳本,該腳本在給定的ul中旋轉li。 我想知道當懸停一個li時是否有可能中斷我的遞歸。 理想情況下,我會創建一個布爾值,以確定是否應繼續遞歸,因為我希望在將來嵌入視頻時中斷遞歸。 我剛開始,所以基本上這就是我所擁有的。

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時,我認為自己是一個巨大的新手,我可能會以一種非常愚蠢的方式解決這個問題,而我卻不知道,任何指針將不勝感激。 干杯。

我將在鼠標懸停時使用公共函數將類似abort的變量設置為true,並在moveSlides的第一行中moveSlides 如果將其設置為true,則只需return該函數即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM