gpt4 book ai didi

Javascript/jQuery 是否可以通过另一个函数跳出一个函数?

转载 作者:行者123 更新时间:2023-11-30 06:05:51 25 4
gpt4 key购买 nike

我正在编写一个脚本,在给定的 ul 中轮换 li。我想知道当其中一个 li 悬停时是否有可能打破我的递归。理想情况下,我会创建一个 bool 值是否应该继续递归,因为我希望将来在我嵌入视频时打破它。我刚刚开始,所以这基本上就是我所拥有的。

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 时,我认为自己是一个巨大的新手,我可能会在不知不觉中以一种非常愚蠢的方式解决这个问题,任何指点都将不胜感激。干杯。

最佳答案

我将在鼠标悬停时使用公共(public)函数将 abort 等变量设置为 true,并在 moveSlides 的第一行检查它。如果将其设置为 true,则简单地从函数中return

关于Javascript/jQuery 是否可以通过另一个函数跳出一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4832752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com