css和js两种方式实现div右移1000px动画
css方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
#box {
width: 100px;
height: 100px;
background-color: brown;
position: relative;
animation: mytest 3s linear 1s;
-webkit-animation: mytest 3s linear 1s;
}
@keyframes mytest {
1% {
left: 0px;
}
20% {
left: 200px;
}
50% {
left: 500px;
}
70% {
left: 700px;
}
100% {
left: 1000px;
}
}
@-webkit-keyframes mytest {
1% {
left: 0px;
}
20% {
left: 200px;
}
50% {
left: 500px;
}
70% {
left: 700px;
}
100% {
left: 1000px;
}
}
</style>
<body>
<div id="box"></div>
</body>
</html>
js原生方法:
<! DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
#box {
width: 100px;
height: 100px;
background-color: red;
position: relative;
}
</style>
<body>
<div id="box"></div>
</body>
</html>
<script>
var oBox = document.getElementById("box")
oBox.onclick = function() {
moveDiv(1000)
}
function moveDiv(data) {
clearInterval(mytimer)
var mytimer = setInterval(function() {
let speed = 10;
if (oBox.offsetLeft != data) {
oBox.style.left = oBox.offsetLeft + speed + 'px'
} else {
clearInterval(mytimer)
}
}, 50)
}
</script>