可拖动对象背后的技术是什么?

发布于 2025-01-03 11:37:08 字数 359 浏览 0 评论 0原文

我正在尝试学习如何通过纯 JavaScript 而不是使用外部库来使 HTML 页面中的 div 可拖动,因此我尝试了一些技术,但未能使其成为适当的可拖动对象。我确信我的代码中遗漏了一些重要的东西,所以我想知道可拖动对象背后的基本思想是什么。我试图通过设置一些 startX 和 startY 位置并使 Div 位置绝对并通过 css 设置 div 的左侧和顶部来实现它

p.style.left = (e.clientX-startX) + 'px';

p.style.top =  (e.clientY-startY) + 'px';

// where p is the element i am trying to make draggable

I am trying to learn how to make a div in an HTML page draggable by pure JavaScript not by using external library so I tried some of mine techniques but I failed to make it a proper draggable object. I am sure I'm missing something important in my code so I want to know what is the basic idea behind draggable object. I was trying to achieve it by setting some startX and startY position and making the Div position absolute and setting the left and top of div by css as

p.style.left = (e.clientX-startX) + 'px';

p.style.top =  (e.clientY-startY) + 'px';

// where p is the element i am trying to make draggable

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

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

发布评论

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

评论(1

ゞ花落谁相伴 2025-01-10 11:37:08

您不应忘记保存 p 的初始位置并每次添加它,以确保您正在进行相对计算。目前,您假设开始拖动时 p 始终位于 (0, 0) 位置。

其次,取消 selectstart 事件可以避免在拖动时创建丑陋的选择。

我对您的代码进行了一些更新: http://jsfiddle.net/rLegF/1/

var p = document.getElementById("p"),
    startX, startY,
    origX, origY,
    down = false;

document.documentElement.onselectstart = function() {
    return false; // prevent selections
};

p.onmousedown = function(e) {
    startX = e.clientX;
    startY = e.clientY;

    origX = p.offsetLeft;
    origY = p.offsetTop;

    down = true;
};

document.documentElement.onmouseup = function() {
    // releasing the mouse anywhere to stop dragging
    down = false;
};

document.documentElement.onmousemove = function(e) {
    // don't do anything if not dragging
    if(!down) return;

    p.style.left = (e.clientX - startX) + origX + 'px';
    p.style.top  = (e.clientY - startY) + origY + 'px';
};

编辑:您还可以结合使用startXorigX,因为您基本上总是在做- startX + origX:< a href="http://jsfiddle.net/rLegF/2/" rel="nofollow">http://jsfiddle.net/rLegF/2/。

然后,您要做的就是计算鼠标相对于元素左上角的位置,然后将该位置设置为新的鼠标位置减去旧的鼠标位置。也许这样更直观一些。

我也清理了一些。

You should not forget to save p's initial position and add it each time to make sure you're doing relative calculations. Currently, you assume p is always at position (0, 0) when starting dragging.

Secondly, cancelling the selectstart event makes for no ugly selection being created when dragging.

I updated your code a bit to this effect: http://jsfiddle.net/rLegF/1/.

var p = document.getElementById("p"),
    startX, startY,
    origX, origY,
    down = false;

document.documentElement.onselectstart = function() {
    return false; // prevent selections
};

p.onmousedown = function(e) {
    startX = e.clientX;
    startY = e.clientY;

    origX = p.offsetLeft;
    origY = p.offsetTop;

    down = true;
};

document.documentElement.onmouseup = function() {
    // releasing the mouse anywhere to stop dragging
    down = false;
};

document.documentElement.onmousemove = function(e) {
    // don't do anything if not dragging
    if(!down) return;

    p.style.left = (e.clientX - startX) + origX + 'px';
    p.style.top  = (e.clientY - startY) + origY + 'px';
};

Edit: You could also combine startX and origX since you're basically always doing - startX + origX: http://jsfiddle.net/rLegF/2/.

What you're then doing is calculating the mouse position with respect to the top left-hand corner of the element, and then set the position to the new mouse position minus that old mouse position. Perhaps it's a little more intuitive that way.

I cleaned up some more as well.

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