<!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>
</head>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 100px;
height: 100px;
background-color: rgb(197, 170, 233);
position: absolute;
}
</style>
<body>
<div></div>
</body>
</html>
<script>
var div_ = document.getElementsByTagName('div')[0];
div_.onmousedown = function (e) {
e = e || window.event;
var x_ = e.offsetX;
var y_ = e.offsetY;
var flg = true;
document.onmousemove = function (e) {
if (flg) {
flg = false;
e = e || window.event;
div_.style.left = e.clientX - x_ + 'px';
div_.style.top = e.clientY - y_ + 'px';
console.log(e.clientX, e.clientY);
setTimeout(function () {
flg = true;
}, 200)
}
}
}
div_.onmouseup = function () {
document.onmousemove = null;
}
</script>