简体   繁体   English

Javascript / jQuery是否可以通过另一个函数来突破一个函数?

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

I'm doing a script that rotates li's in a given ul. 我正在做一个脚本,该脚本在给定的ul中旋转li。 I'd like to know whether it is possible to break a recursion I have when one of the li's is hovered. 我想知道当悬停一个li时是否有可能中断我的递归。 Ideally I'd create a boolean whether the recursion should continue or not, as I'd like to have this to break when I have a video embed in it in the future. 理想情况下,我会创建一个布尔值,以确定是否应继续递归,因为我希望在将来嵌入视频时中断递归。 I've just started it so this is basically what I've got. 我刚开始,所以基本上这就是我所拥有的。

HTML: 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: 使用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 CSS

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

Also note that I consider myself a huge newbie when it comes to Javascript, I might be going around this in a very idiotic way without me knowing it, any pointers would be appreciated. 还要注意,当我谈到Javascript时,我认为自己是一个巨大的新手,我可能会以一种非常愚蠢的方式解决这个问题,而我却不知道,任何指针将不胜感激。 Cheers. 干杯。

I woul set a variable like abort to true using a public function in the mouseover and check it in the first line of moveSlides . 我将在鼠标悬停时使用公共函数将类似abort的变量设置为true,并在moveSlides的第一行中moveSlides If it's set to true simply return out of the function. 如果将其设置为true,则只需return该函数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM