其他分享
首页 > 其他分享> > 拖动效果

拖动效果

作者:互联网

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>发光输入框</title>
<style type="text/css">
#box {
position: absolute;
width: 100px;
height: 100px;
top: 10px;
left: 10px;
background: red;
}
</style>
</head>
<body>

<div id="box"></div>

</body>
<script>
let box = document.getElementById('box');
document.onmousedown = function (e) {
let disx = e.pageX - box.offsetLeft;
let disy = e.pageY - box.offsetTop;
document.onmousemove = function (e) {
box.style.left = e.pageX - disx + 'px';
box.style.top = e.pageY - disy + 'px';
}
//释放鼠标按钮,将事件清空,否则始终会跟着鼠标移动
document.onmouseup = function () {
document.onmousemove = document.onmouseup = null;
}
}
</script>
</html>

标签:box,function,效果,拖动,disy,disx,let,document
来源: https://www.cnblogs.com/1024L/p/15406188.html