49 lines
1.2 KiB
HTML
49 lines
1.2 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
<title>Document</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
div {
|
|
width: 100px;
|
|
height: 100px;
|
|
background: red;
|
|
position: absolute;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<input type="button" value="移动到400px的位置" id="btn1">
|
|
<div id="dv"></div>
|
|
<script type='text/javascript'>
|
|
var btn1 = document.getElementById('btn1')
|
|
var dv = document.getElementById('dv')
|
|
var timer
|
|
btn1.onclick = function() {
|
|
//让div移动到400px的位置
|
|
// dv.style.left = '400px';
|
|
//一步一步走 匀速
|
|
var step = 1
|
|
//每隔一段时间向右走一步 创建定时器
|
|
var current = dv.offsetLeft
|
|
clearInterval(timer);
|
|
timer = setInterval(function() {
|
|
current += step;
|
|
//把最新的赋给div
|
|
dv.style.left = current +'px'
|
|
if(current > 400) {
|
|
clearInterval(timer);
|
|
current = 400
|
|
// return
|
|
}
|
|
}, 10)
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |