Javascript 循环动画功能
如何使用jquery循环动画函数?我有Javascript 循环动画功能,javascript,jquery,html,css,Javascript,Jquery,Html,Css,如何使用jquery循环动画函数?我有 <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script type="text/javascript"> $(document
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
$(".b").click(function(){
var a=$(".abb");
a.animate({top:'100px',left:'400px'},"slow");
a.animate({top:'20px',left:'500px'},"slow");
a.animate({top:'500px',left:'100px'},"slow");
a.animate({top:'100px',left:'800px'},"slow");
a.animate({top:'200px',left:'100px'},"slow");
a.animate({top:'300px',left:'0px'},"slow");
a.animate({top:'600px',left:'300px'},"slow");
a.animate({top:'100px',left:'700px'},"slow");
a.animate({top:'300px',left:'100px'},"slow");
});
});
</script>
</head>
<body>
<button class="b"> click </button>
<div class="abb" style="width:100px;height:100px;background:#9F0;position:absolute;border-radius:70px;box-shadow:#000 1px 1px 3px 2px;"></div>
</body>
</html>
$(文档).ready(函数(){
$(“.b”)。单击(函数(){
var a=$(“.abb”);
a、 制作动画({top:'100px',left:'400px'},“slow”);
a、 设置动画({top:'20px',left:'500px'},“slow”);
a、 设置动画({top:'500px',left:'100px'},“slow”);
a、 制作动画({top:'100px',left:'800px'},“slow”);
a、 设置动画({top:'200px',left:'100px'},“slow”);
a、 设置动画({top:'300px',left:'0px'},“slow”);
a、 制作动画({top:'600px',left:'300px'},“slow”);
a、 制作动画({top:'100px',left:'700px'},“slow”);
a、 制作动画({top:'300px',left:'100px'},“slow”);
});
});
点击
我希望上面的脚本能够持续工作。请协助试试这个。这将在每5秒增加一次后重新启动动画
<script type="text/javascript">
$(document).ready(function(){
$(".b").click(function(){
setInterval(animate_me,5000);
});
});
function animate_me()
{
var a=$(".abb");
a.animate({top:'100px',left:'400px'},"slow");
a.animate({top:'20px',left:'500px'},"slow");
a.animate({top:'500px',left:'100px'},"slow");
a.animate({top:'100px',left:'800px'},"slow");
a.animate({top:'200px',left:'100px'},"slow");
a.animate({top:'300px',left:'0px'},"slow");
a.animate({top:'600px',left:'300px'},"slow");
a.animate({top:'100px',left:'700px'},"slow");
a.animate({top:'300px',left:'100px'},"slow");
}
</script>
$(文档).ready(函数(){
$(“.b”)。单击(函数(){
设置间隔(动画,5000);
});
});
函数animate_me()
{
var a=$(“.abb”);
a、 制作动画({top:'100px',left:'400px'},“slow”);
a、 设置动画({top:'20px',left:'500px'},“slow”);
a、 设置动画({top:'500px',left:'100px'},“slow”);
a、 制作动画({top:'100px',left:'800px'},“slow”);
a、 设置动画({top:'200px',left:'100px'},“slow”);
a、 设置动画({top:'300px',left:'0px'},“slow”);
a、 制作动画({top:'600px',left:'300px'},“slow”);
a、 制作动画({top:'100px',left:'700px'},“slow”);
a、 制作动画({top:'300px',left:'100px'},“slow”);
}
jQuery/JS
您也可以通过使用标记
Move
true/false来移动和停止,就像我使用的Deryck
HTML
<button class="b">Move</button><button class="s">Stop</button>
<div class="abb" style="width:100px;height:100px;background:#9F0;position:absolute;border-radius:70px;box-shadow:#000 1px 1px 3px 2px;"></div>
参见你可以在它的
complete()
内部使用一个setTimeout()
来再次调用它。你能给我看一下例子吗参考我的答案。享受:)使用SetInterval
。setInterval
代码每6000毫秒执行一次,而setTimeout等待6000毫秒,运行函数,需要几毫秒,然后设置另一个超时。因此,等待时间实际上大于6000ms。这一推理事实上证明了为什么setInterval()
不适合在这里使用。如果您按您所说的“6000毫秒”调用函数,它将继续排队,并可能干扰其他代码。使用setTimeout
调用自身可确保每次执行都不会在触发前等待最后一次执行完成。
<button class="b">Move</button><button class="s">Stop</button>
<div class="abb" style="width:100px;height:100px;background:#9F0;position:absolute;border-radius:70px;box-shadow:#000 1px 1px 3px 2px;"></div>
var move;
$(document).ready(function(){
$(".b").click(function(){
move = true;
setInterval(animate_me,6000);
});
$(".s").click(function(){
move = false;
});
});
function animate_me()
{
if(move===true)
{
var a=$(".abb");
a.animate({top:'100px',left:'400px'},"slow");
a.animate({top:'20px',left:'500px'},"slow");
a.animate({top:'500px',left:'100px'},"slow");
a.animate({top:'100px',left:'800px'},"slow");
a.animate({top:'200px',left:'100px'},"slow");
a.animate({top:'300px',left:'0px'},"slow");
a.animate({top:'600px',left:'300px'},"slow");
a.animate({top:'100px',left:'700px'},"slow");
a.animate({top:'300px',left:'100px'},"slow");
}
}