相当于 jQuery.draggable() 的原生 JavaScript

发布于 2024-12-10 12:48:03 字数 179 浏览 0 评论 0原文

是否有相当于 jQuery 的draggable 的原生javascript 代码? 我不需要所有功能,但

  1. 能够从手柄拖动模式窗口。
  2. 在开始和结束时 事件,因为我需要在拖动时制作一个覆盖层以防止 鼠标位于模式窗口内的 iframe 上。
  3. 文档上只有一个可拖动的项目。

Is there a native javascript code equivalent to jQuery's draggable?
I don't need all the features but

  1. being able to drag a modal window from a handle.
  2. 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.
  3. there will be only one draggable item on the document.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空名 2024-12-17 12:48:04

使用事件 dragstartdragend。将模态的位置与鼠标的 x 和 y 坐标联系起来。

Use the events dragstart and dragend. Tie the position of the modal relevant to the mouse's x and y coordinates.

云淡月浅 2024-12-17 12:48:04

有 HTML5 dnd api,但由于与我交谈过的人都不喜欢它,而且所有较旧的浏览器都不支持它,所以我想说您在 jQuery 中找到了一个很好的解决方案。

There is the HTML5 dnd api, but since nobody i've talked to liked it in any way and none of the older browsers support it,I would say you found a good solution in jQuery.

淡淡離愁欲言轉身 2024-12-17 12:48:04

使用看起来比 jQuery 更干净的 HTML5 方法:

<div draggable="true"
  ondragstart="event.dataTransfer.setData('text/plain', '12345')">
drag me
</div>

<div ondragover="return false;" 
  ondrop="this.innerHTML=event.dataTransfer.getData('text/plain')">
drop on me
</div>

Use the HTML5 methods which seem cleaner than jQuery's:

<div draggable="true"
  ondragstart="event.dataTransfer.setData('text/plain', '12345')">
drag me
</div>

<div ondragover="return false;" 
  ondrop="this.innerHTML=event.dataTransfer.getData('text/plain')">
drop on me
</div>
不及他 2024-12-17 12:48:04

尽管问题指出的是原生 JavaScript,但考虑到 @Moe Sweet 评论说 jQuery 已启用,我将做出回应。

拖放是一个特定的事件链。这并不是非常困难,但可能具有挑战性。

简单地说,拖放是以下步骤/事件:

  1. 鼠标按下,启用拖动
  2. 鼠标移动,继续拖动
  3. 鼠标松开,禁用拖动

简单的解决方案是使用这些事件: mousedownmousemovemouseup

$(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 事件期间才真正需要运动,因为在 mouseupmousedown 事件中鼠标不会移动。

有一个类似的问题,其中我发布了 jQuery.dragondrop 插件的代码

Although the question states native JavaScript, I'm going to respond given that @Moe Sweet commented that jQuery was enabled.

Drag and drop is a particular event chain. It's not obscenely difficult, but it can be challenging.

Simply put, drag and drop is the following steps/events:

  1. Mouse pressed down, dragging is enabled
  2. Mouse is moved, dragging continues
  3. Mouse is let up, dragging is disabled

The simple solution is to use those events: mousedown, mousemove, and 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);
}

A point that should be noticed is that the events following the mousedown need to be bound to the window so that they are caught if the mouse has left the original element while being held down.

The "stuff" part of the code is where you'll need to check the screen coords of the element vs the screen coords of the mouse and move things appropriately. Motion is only really necessary during the mousemove event as the mouse wont have moved for a mouseup or mousedown event.

There is a similar question where I posted my code for a jQuery.dragondrop plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文