计时器之旋转
2019-05-13 本文已影响0人
椋椋夜色
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> 计时器之旋转 </title>
<style>
body{
background-color: rgb(5, 0, 0);
}
input {
margin: 0 auto;
}
.box {
width: 25px;
height: 660px;
text-align: center;
line-height: 130px;
background: linear-gradient(yellow,red,yellow );
margin: 200px auto;
}
</style>
</head>
<body>
<input type="button" value="转起来" id="ben">
<input type="button" value="停下来" id="ben2">
<div class="box">爱你一万年</div>
<script>
// 找到元素
// var box = document.getElementsByClassName('box')[0];
var box = document.querySelector('.box');
// 添加点击事件
var timerID;
document.getElementById('ben').onclick = function () {
timerID = setInterval(function() {
box.style.transform += "rotate(30deg)";
},10)
};
document.getElementById('ben2').onclick = function(){
clearInterval(timerID);
}
</script>
</body>
</html>