MyDragLibrary.js

本文介绍了一个简单的拖拽面板功能实现方法,通过JavaScript监听鼠标事件完成元素的拖动。具体包括鼠标按下时记录初始位置差值、鼠标移动时更新元素位置及鼠标抬起时取消事件监听等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

var MyDragHandler = { DragPanelId: "divContext", _idiffx: 0, _idiffy: 0, _Div: null, AttachDrag: function(dragId) { if (dragId) MyDragHandler._Div = document.getElementById(dragId); else MyDragHandler._Div = document.getElementById(MyDragHandler.DragPanelId); document.body.onmousedown = MyDragHandler._handleMouseDown; }, _handleMouseDown: function() { var oEvent = window.event; if (MyDragHandler._Div) { MyDragHandler._idiffx = oEvent.clientX - MyDragHandler._Div.offsetLeft; MyDragHandler._idiffy = oEvent.clientY - MyDragHandler._Div.offsetTop; document.body.onmousemove = MyDragHandler._handleMouseMove; document.body.onmouseup = MyDragHandler._handleMouseUp; } }, _handleMouseMove: function() { var oEvent = window.event; MyDragHandler._Div.style.left = oEvent.clientX - MyDragHandler._idiffx; MyDragHandler._Div.style.top = oEvent.clientY - MyDragHandler._idiffy; MyDragHandler._Div.style.cursor = "move"; }, _handleMouseUp: function() { document.body.onmousemove = null; document.body.onmouseup = null; MyDragHandler._Div.style.cursor = "default"; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值