我在w3schools中找到了这个例子,并且想要将它的动画修改为我喜欢的。基本上它有两个div,一个是容器,另一个是动画。最初动画div可以从上到下或从上到下等一个方向。但是我想要发生的是,只要它已经从上到下进行动画,我希望它从下往上移回来。如何实现这一目标?如何在JavaScript中动画?
下面是代码:
<!DOCTYPE html>
<html>
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 100px;
height: 100px;
position: absolute;
left: 145px;
background-color: red;
}
</style>
<body>
<p>
<button onclick="Move()">Click</button>
</p>
<div id ="container">
<div id ="animate"></div>
</div>
<script>
function Move() {
var elem = document.getElementById("animate");
var pos = 0;
var id = setInterval(frame, 5);
function frame() {
if (pos < 300) {
pos++;
elem.style.top = pos + 'px';
elem.style.bottom = pos + 'px';
}
}
}
</script>
</body>
</html>
这是动画的一个很大的参考http://robertpenner.com/easing/其余https://www.google.es/search?q=robert+penner+动画+ javascript&oq = robert + penner + animation + javascript&aqs = chrome..69i57.6743j0j1&sourceid = chrome&ie = UTF-8#q = +动画+ javascript –
您必须设置一个“方向”,否则如果您只查看垂直位置,没有工作。 – evolutionxbox