【发布时间】:2021-04-16 07:19:51
【问题描述】:
我在使用 jQuery stop() 函数停止我的动画的无限循环 时遇到问题。动画队列如下:
- 将 div 框向右移动 300 像素
- 将 div box 背景颜色更改为绿色
- 双倍大小的 div 框
- 将div 框移回原来的位置(marginLeft 0 px)
- 将 div box 的大小改回原来的大小
- 将 div box 背景颜色改回黄色
div box的原始属性:
- marginLeft: 0 像素
- 背景颜色:黄色
- 宽度和高度:100 像素
这是我尝试过的代码:
<html>
<head>
<style>
#box
{
background: yellow;
height: 100px;
width: 100px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#startBtn").click(function loop(){
$("#box").animate({marginLeft: '300px'});
$("#box").animate({backgroundColor: 'green'});
$("#box").animate({width: '200px',height: '200px'});
$("#box").animate({marginLeft: '0px'});
$("#box").animate({width: '100px',height: '100px'});
$("#box").animate({backgroundColor: 'yellow'});
setInterval(loop,0000);
});
$("#stopBtn").click(function(){
$("#box").stop(true,true); // stop animation & immediately go to end of animation
});
});
</script>
</head>
<body>
<button id="startBtn">Start</button>
<button id="stopBtn">Stop</button>
<br/> <br/>
<div id="box"></div>
</body>
</html>
根据我对 jQuery stop() 方法的了解,第一个参数表示是否清除所有动画队列。第二个表示是否立即结束当前动画。
我不知道为什么当我执行 stop(true,true) 函数时,动画仍然无限继续。 (不停)
我的stopBtn jQuery 代码应该停止执行任何动画并在点击停止按钮之前立即转到当前正在执行的动画结束。
请帮忙。
【问题讨论】:
标签: jquery