<!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:0px;
padding: 0px;
list-style: none;
}
div{
width: 400px;
margin:0px auto;
position: relative;
background:lime;
}
img:nth-of-type(1){
width: 400px;
}
img:nth-of-type(2){
position: absolute;
left: 38%;
top: 48%;
/* translate如果移动是百分比,参照物是自身 */
/* transform: translate(-50%,-50%); */
margin-top: -74px;
margin-left: -74px;
width: 148px;
}
</style>
</head>
<body>
<div>
<img src="images/pic.png" alt="">
<img src="images/row.png" id="row" alt="" >
</div>
<script>
var num=0;
var timer;
var row=document.getElementById('row');
row.onclick=function(){
clearInterval(timer)
// 初始化
row.style.transform='rotate(0deg)'
// 确定停止的时候的角度
var deg=360*5+parseInt(Math.random()*8)*45
console.log(deg+'这是deg')
timer=setInterval(function(){
num+=45;
console.log(num+'这是num')
// 旋转
row.style.transform='rotate('+num+'deg)'
if(num>=deg){
clearInterval(timer);
num=0;
}
},50)
}
</script>
</body>
</html>
效果图如下: