<!DOCTYPE html><html lang="en"><body><span id="bar" style="position: relative;border: 1px solid red;">长按拖拽</span><script>const mydiv = document.getElementById("bar");// 需要补充一个开关mydiv.onmousedown = function (event) {document.onmousemove = function () {// x 和 y 的最大值和最小值略let x2 = window.event.clientX,y2 = window.event.clientY;mydiv.style.top = `${y2}px`mydiv.style.left = `${x2}px`}}// 拖拽完成 解绑mousemove事件window.onmouseup = function () {document.onmousemove = null}</script></body></html>
