gpt4 book ai didi

javascript - 从头开始恢复动画 css3

转载 作者:行者123 更新时间:2023-11-28 08:58:26 26 4
gpt4 key购买 nike

我试图制作一个按钮来停止 css3 动画并再次启动它我试过这个功能object.style.animationPlayState="paused";

但问题是我想停止并从头开始重新运行它而不是从最后一个地方恢复它。

我还尝试在停止之前操作 object.style.left 值,也从它所在的最后一个位置开始。

我最后一次尝试是将计数设置为 0,但到目前为止没有成功

关于这个问题有什么想法吗?

代码

<html>
<head>
<title>Move 3 pic's</title>
</head>
<style>

#div1{position: relative; top:100px;}
#div2{position: relative ; top:100px;left:290px;}
#div3{position: relative ; top:250px; left:50px;}


#divmain
{
width: 400px; height:300px; border: 10px solid navy; margin: 5px;

}

.animate1
{
animation: movingdiv1 5s infinite alternate;
animation-timing-function:linear;
}

.animate2
{
animation: movingdiv2 5s infinite alternate;
animation-timing-function:linear;
}

.animate3
{
animation: movingdiv3 5s infinite alternate;
animation-timing-function:linear;
}

@-moz-keyframes movingdiv1
{
from {left: 0px;}
to {left: 300px;}
}

@-moz-keyframes movingdiv2
{
from {left: 300px;}
to {left: 0px;}
}

@-moz-keyframes movingdiv3
{
from {top: 240px;}
to {top: 0px;}
}
</style>
<script>
var x=0;
function start()
{
document.getElementById("div1").style.animationIterationCount="infinite";
document.getElementById("div2").style.animationIterationCount="infinite";
document.getElementById("div3").style.animationIterationCount="infinite";
if(x==1)
{
document.getElementById("start").value="Stop";
document.getElementById("div1").style.animationPlayState="running";
document.getElementById("div2").style.animationPlayState="running";
document.getElementById("div3").style.animationPlayState="running";
x=0;
}
else
{
document.getElementById("start").value="Start";
document.getElementById("div1").style.animationPlayState="paused";
document.getElementById("div2").style.animationPlayState="paused";
document.getElementById("div3").style.animationPlayState="paused";
x=1;
}
}


function reset()
{

/*what i tried here*/
document.getElementById("div1").style.animationPlayState="paused";
document.getElementById("div2").style.animationPlayState="paused";
document.getElementById("div3").style.animationPlayState="paused";

document.getElementById("div1").style.left="0px";
document.getElementById("div2").style.left="297px";
document.getElementById("div3").style.top="250px";

document.getElementById("div1").style.animationIterationCount="0";
document.getElementById("div2").style.animationIterationCount="0";
document.getElementById("div3").style.animationIterationCount="0";
}
</script>
<body>
<div id=divmain>
<span class="animate1" id=div1>
<img src="icon1.gif" id="icon1" width="50" height="50"/></span>
<span class="animate2" id=div2>
<img src="icon2.gif" id="icon2" width="50" height="50"/></span>
<span class="animate3" id=div3><img src="top.jpg" id="icon3" width="50" height="50"/></span>
</div>
<input id=start type=button onClick=start() value='Stop'>
<input id=reset type=button onClick=reset() value='Reset'>
</body>

最佳答案

如果您想从头开始重新运行动画,请删除动画属性中的“alternate”。

.animate1
{
animation: movingdiv1 5s infinite;
animation-timing-function:linear;
}

.animate2
{
animation: movingdiv2 5s infinite;
animation-timing-function:linear;
}

.animate3
{
animation: movingdiv3 5s infinite;
animation-timing-function:linear;
}

编辑:

可能我之前没有理解你的问题。每当单击按钮时,您想从头开始停止并重新运行它。为此,我修改了启动函数()。每当按下停止按钮时,动画类就会从移动的 div 中删除。当按下开始按钮时,将添加动画类。

 function start()
{
document.getElementById("div1").style.animationIterationCount="infinite";
document.getElementById("div2").style.animationIterationCount="infinite";
document.getElementById("div3").style.animationIterationCount="infinite";
if(x==1)
{
document.getElementById("start").value="Stop";
document.getElementById("div1").className = "animate1";
document.getElementById("div2").className = "animate2";
document.getElementById("div3").className = "animate3";
x=0;
}
else
{
document.getElementById("start").value="Start";
document.getElementById("div1").className = "";
document.getElementById("div2").className = "";
document.getElementById("div3").className = "";
x=1;
}
}

关于javascript - 从头开始恢复动画 css3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com