jquery - jQuery停止循环动画方法不起作用
问题描述
我在使用 jQuery stop() 函数停止动画的无限循环时遇到问题。动画队列如下:
- 将 div框向右移动 300 px
- 将div框背景颜色更改为绿色
- div框的两倍大小
- 将 div框移回原始位置(marginLeft 0 px)
- 将 div框的大小更改回原始大小
- 将div框背景颜色更改回黄色
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 代码应该停止执行任何动画,并在点击停止按钮之前立即转到当前正在执行的动画的结尾。
请帮忙。
解决方案
这是我找到的解决方案。一旦stopBtn
执行,它将停止当前正在执行的动画并转到该动画的结尾。
首先,创建一个animationList()
包含所有动画队列的 JavaScript 函数。
其次,声明一个变量animationQueue
。这将存储setInterval()
请求。
三、创建jQuerystartBtn
点击功能。这个函数启动里面的动画队列,animationList()
使用setInterval()
它将每个动画之间的间隔设置为 1 秒。
最后,创建一个 jQuerystopBtn
点击函数。您必须使用clearInterval()
方法清除动画之间设置的 1 秒间隔。然后,执行jQuery stop(true,true)
,清空里面定义的动画队列animationList()
(第一个参数为true),停止当前动画,走到动画的结尾(第二个参数为true)。
<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(){
var animinationQueue;
$("#startBtn").click(function (){
animinationQueue
= setInterval(animationList,1000);
});
$("#stopBtn").click(function(){
clearInterval(animinationQueue);
$("#box").stop(true,true); // stop all animination & go to the end of animination
});
function animationList()
{
$("#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'});
}
});
</script>
</head>
<body>
<button id="startBtn">Start</button>
<button id="stopBtn">Stop</button>
<br/> <br/>
<div id="box"></div>
</body>
</html>
推荐阅读
- kubernetes - 如何使用 Kubernetes API 获取特定 Kubernetes 集群中所有命名空间的列表?
- wolkenkit - 如何使用/消费来自 wolkenkit-eventstore 的事件流
- php - Laravel 5.8 更新 mysql json 列转换为数组更改数据存储格式
- javascript - 错误类型错误:无法读取未定义的属性“有效”
- linux - 使用 sed 命令将行替换为正则表达式
- java - java - 如何在java swing中创建3个按钮以占据它们添加到的面板的1/3?
- elasticsearch - 在 Kibana 7/6.7.2 中为 DSL 中大小为 1 的最新 _id 设置过滤器
- reactjs - 在使用样式化组件时尝试理解“theme="[object Object]" 的含义
- angular - 子组件输入变量的setter/ngOnChanges方法只调用一次?
- neo4j - 从neo4j中的json文件导入和创建多个标签和关系