如何中断/暂停内容滑块的无限循环 Jquery 函数?
我有一个页面,我使用 jquery fadeout() & 循环浏览一组列表元素。淡入淡出()。
一切工作正常,除了我现在想要这样:
- 用户可以单击代表旋转中的 li 的单独元素。
- 当他们点击该元素时,当前的动画循环停止,
- 相应的内容li被加载,
- 动画恢复。
我相信我需要使用 jquery 队列来做到这一点,但想知道是否有一个我忽略的更简单的解决方案。
这是我的代码:
$(document).ready(function () {
var j = 0;
var fadetime = 700;
var delay = 3000; //millisecond delay between cycles
function cycleThru() {
var jmax = $("ul#rotator li").length - 1;
$("ul#rotator_pager li:eq(" + j + ")").css("background-color", "#660000");
$("ul#rotator li:eq(" + j + ")").fadeIn(fadetime).delay(delay);
$("ul#rotator li:eq(" + j + ")").fadeOut(fadetime, function () {
$("ul#rotator_pager li:eq(" + j + ")").css("background-color", "");
(j == jmax) ? j = 0 : j++;
cycleThru();
});
};
//Setup the clickers on the pager boxes.
$("ul#rotator_pager li").click(function () {
//Switch to this list element and resume animation cycle.
});
cycleThru();
});
对应的 HTML --
<ul id="rotator">
<li id="first">
<div><!--HTML Goes HERE--></div>
</li>
<li>
<div><!--HTML Goes HERE--></div>
</li>
<li>
<div><!--HTML Goes HERE--></div>
</li>
</ul>
<ul id="rotator_pager">
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
I have a page where I am cycling through a set of list elements using jquery fadeout() & fadein().
Everything works fine, except I would now like to make it so:
- the user can click a separate element that represents a li in the rotation.
- When they click the element, the current animation cycle is stopped,
- the corresponding content li is loaded
- the animation resumes.
I believe I need to do this with jquery queues, but was wondering if there was an easier solution I'm overlooking.
Here is my code:
$(document).ready(function () {
var j = 0;
var fadetime = 700;
var delay = 3000; //millisecond delay between cycles
function cycleThru() {
var jmax = $("ul#rotator li").length - 1;
$("ul#rotator_pager li:eq(" + j + ")").css("background-color", "#660000");
$("ul#rotator li:eq(" + j + ")").fadeIn(fadetime).delay(delay);
$("ul#rotator li:eq(" + j + ")").fadeOut(fadetime, function () {
$("ul#rotator_pager li:eq(" + j + ")").css("background-color", "");
(j == jmax) ? j = 0 : j++;
cycleThru();
});
};
//Setup the clickers on the pager boxes.
$("ul#rotator_pager li").click(function () {
//Switch to this list element and resume animation cycle.
});
cycleThru();
});
Corresponding HTML --
<ul id="rotator">
<li id="first">
<div><!--HTML Goes HERE--></div>
</li>
<li>
<div><!--HTML Goes HERE--></div>
</li>
<li>
<div><!--HTML Goes HERE--></div>
</li>
</ul>
<ul id="rotator_pager">
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使您的递归函数调用依赖于全局布尔变量:
Make your recursive function call depend on a global Boolean variable:
您可以将动画设置为 var,这样您就可以在单击时清除它。
You can set the animation as a var so you can clear it on click.
所以,我离开它几分钟并弄清楚了。您确实需要使用 Jquery 队列,但它并不像我想象的那么复杂。这是其他人的解决方案:
});
基本上,当您单击列表元素时,它会:
So, I stepped away from it for a few mins and figured it out. You do need to use the Jquery queue's but it was not nearly as complicated as I thought. Here's the solution for others:
});
Basically, when you click on the list element, it :