Javascript/jQuery 是否可以通过另一个函数突破一个函数?
我正在编写一个在给定 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 时,我认为自己是一个巨大的新手,我可能会在不知情的情况下以一种非常愚蠢的方式解决这个问题,任何指示将不胜感激。干杯。
I'm doing a script that rotates li's in a given ul. I'd like to know whether it is possible to break a recursion I have when one of the li's is hovered. 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:
<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;
}
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. Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用鼠标悬停中的公共函数将像
abort
这样的变量设置为 true,并在moveSlides
的第一行中检查它。如果它设置为 true,则只需从函数中返回
即可。I woul set a variable like
abort
to true using a public function in the mouseover and check it in the first line ofmoveSlides
. If it's set to true simplyreturn
out of the function.