<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
.box{
position:absolute;
top:100px;
left:0;
width:100px;
height:100px;
background-color:skyblue;
}
</style>
</head>
<body>
<input type="button" value="开始将下面的元素向右移动" id="start">
<div class="box" id="box">
</div>
<script>
var start = document.getElementById("start");
var box = document.getElementById("box");
//点击开始按钮,让box向右运动
//全局变量存储left的属性值,会每次发生变化
//信号量,初始值必须与属性初始值保持一致
var nowleft = 0;
//定时器,制作运动过程
start.onclick = function(){
setInterval(function(){
//信号量进行自加
nowleft += 1;
//将最新的nowleft的值赋值给元素的css属性
box.style.left = nowleft + "px";
},100);
};
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
.box{
position:absolute;
top:100px;
left:0;
width:100px;
height:100px;
background-color:skyblue;
}
</style>
</head>
<body>
<input type="button" value="开始将下面的元素向右移动" id="start">
<div class="box" id="box">
</div>
<script>
var start = document.getElementById("start");
var box = document.getElementById("box");
//点击开始按钮,让box向右运动
//全局变量存储left的属性值,会每次发生变化
//信号量,初始值必须与属性初始值保持一致
var nowleft = 0;
//定时器,制作运动过程
start.onclick = function(){
setInterval(function(){
//信号量进行自加
nowleft += 1;
//将最新的nowleft的值赋值给元素的css属性
box.style.left = nowleft + "px";
},100);
};
</script>
</body>
</html>