<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
position: absolute;
width: 200px;
height: 200px;
background-color: pink;
}
</style>
</head>
<body>
<div></div>
<script>
var oDiv = document.getElementsByTagName('div')[0];
oDiv.onmousedown = function (e) {
var startX = e.clientX;
var startY = e.clientY;
var startLeft = oDiv.offsetLeft;
var startTop = oDiv.offsetTop;
document.onmousemove = function (ev) {
oDiv.style.left = startLeft + ev.clientX - startX + 'px';
oDiv.style.top = startTop + ev.clientY - startY + 'px';
document.onmouseup = function (ev1) {
document.onmouseup = null;
document.onmousemove = null;
document.onmousedown = null;
};
return false
}
}
</script>
</body>
</html>