解决移动元素时,乱闪的问题,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
#box {
position: relative;
width: 100vw;
height: 100vh;
background: red;
}
#index {
width: 100px;
height: 100px;
border-radius: 50%;
background: green;
position: absolute;
top: 0;
left: 0;
}
#index2 {
width: 100px;
height: 100px;
background: rgba(0, 0, 0, 0);
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div id="box">
<div id="index"></div>
<div id="index2"></div>
</div>
<script type="text/javascript">
var box = document.getElementById("box");
var index = document.getElementById("index");
box.onmousemove = function (event) {
var a = window.event || event;
var x = a.offsetX - 50;
var y = a.offsetY - 50;
document.title = (x + "|" + y);
index.style.left = x + "px";
index.style.top = y + "px";
}
</script>
</body>
</html>