【发布时间】:2020-09-16 14:48:52
【问题描述】:
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: rgb(240, 240, 231);
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
#animate1 {
width: 50px;
height: 50px;
left: 350px;
top: 2px;
position: absolute;
background-color: rgb(43, 1, 1);
}
</style>
<p>
<button onclick="myMove()">Click Me</button>
</p>
<div id="container">
<div id="animate"></div>
<div id="animate1"></div>
</div>
<script>
function myMove() {
var elem = document.getElementById("animate");
var elem1 = document.getElementById("animate1");
var pos = 0;
var pos1 = 0;
var id = setInterval(frame, 5);
var id1 = setInterval(frame1, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
}
function frame1() {
if (pos1 == 350) {
clearInterval(id1);
} else {
pos1++;
elem1.style.top = pos1 + "px";
elem1.style.right = pos1 + "px";
}
}
}
</script>
在上面的代码中,我有 2 个立方体(div)。单击按钮时,左立方体应从左到右移动到容器的对角线末端,右立方体应该从右到左对角线移动到容器的末端。但只有左立方体从左向右移动,右立方体从上到下移动。 我希望右立方体从右到左对角线移动到容器的末尾。这是我的沙盒链接。 https://codesandbox.io/s/autumn-sun-uz961?file=/index.html
【问题讨论】:
标签: javascript html css animation css-selectors