<!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>左右移动方框</title>
<script>
window.οnlοad=function(){
var box = document.getElementById('box');
var iLeft = 0;
var Speed = 3;
//定义一个定时器
var Timer = setInterval(Move,5);
function Move(){
iLeft +=Speed;
if (iLeft>1300){
Speed = -3
}
if(iLeft<0){
Speed = 3
}
box.style.left = iLeft + 'px'
}
}
</script>
<style>
#box {
width: 100px;
height: 100px;
background-color: hotpink;
position: fixed;
}
</style>
</head>
<body>
<div id="box"></div>
</body>
</html>
使用JavaScript左右移动方框
最新推荐文章于 2022-09-21 15:35:57 发布