<!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>
#box {
width: 60px;
height: 60px;
background: salmon;
border-radius: 50%;
position: absolute;
top: 50px;
left: 20px;
}
</style>
</head>
<body>
<button id="btn">点击向右移动</button>
<button id="btn-stop">暂停</button>
<div id="box"></div>
<script>
var o = document.getElementById("box");
var speed = 1;
var t;
//点击向右移动事件
document.getElementById('btn').onclick = function () {
clearInterval(t);
//小球自行移动
t=setInterval(moveright, 50);
// console.log(t);
}
//点击事件 停止移动
document.getElementById('btn-stop').onclick = function () {
clearInterval(t);
}
//小球向右移动
function moveright() {
var l = window.getComputedStyle(o, null).left;
l = parseInt(l);
//修改left的值
o.style.left = l + speed + "px";
}
</script>
</body>
</html>
javascript 制作一个移动的小球
猜你喜欢
转载自blog.csdn.net/W859265708/article/details/81038223
今日推荐
周排行