相当于 jQuery.draggable() 的原生 JavaScript
是否有相当于 jQuery 的draggable 的原生javascript 代码? 我不需要所有功能,但
- 能够从手柄拖动模式窗口。
- 在开始和结束时 事件,因为我需要在拖动时制作一个覆盖层以防止 鼠标位于模式窗口内的 iframe 上。
- 文档上只有一个可拖动的项目。
Is there a native javascript code equivalent to jQuery's draggable?
I don't need all the features but
- being able to drag a modal window from a handle.
- on start and on end
events as I need to make an overlay layer while dragging to prevent
the mouse being upon the iframe inside the modal window. - there will be only one draggable item on the document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
不及他2024-12-17 12:48:04
尽管问题指出的是原生 JavaScript,但考虑到 @Moe Sweet 评论说 jQuery 已启用,我将做出回应。
拖放是一个特定的事件链。这并不是非常困难,但可能具有挑战性。
简单地说,拖放是以下步骤/事件:
- 鼠标按下,启用拖动
- 鼠标移动,继续拖动
- 鼠标松开,禁用拖动
简单的解决方案是使用这些事件: mousedown
、mousemove
和 mouseup
$(anElement).mousedown(foodown);
function foodown(){
$(window).mousemove(foomove).mouseup(fooup);
//stuff
}
function foomove(){
//stuff
}
function fooup(){
//stuff
$(window).unbind('mousemove', foomove).unbind('mouseup', fooup);
}
需要注意的一点是,mousedown
后面的事件需要绑定到 window
这样他们就会被抓住如果鼠标在按住时离开了原始元素。
代码的“东西”部分是您需要检查元素的屏幕坐标与鼠标的屏幕坐标并适当移动物体的地方。只有在 mousemove
事件期间才真正需要运动,因为在 mouseup
或 mousedown
事件中鼠标不会移动。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
使用事件
dragstart
和dragend
。将模态的位置与鼠标的 x 和 y 坐标联系起来。Use the events
dragstart
anddragend
. Tie the position of the modal relevant to the mouse's x and y coordinates.