简体   繁体   English

使用jQuery的可拖动div,如何应用元素的点击偏移量?

[英]Draggable div with jQuery, how to apply clicked offset of element?

Title might be little confuse, I'll explain. 标题可能有点混乱,我会解释。 I made a draggable div with jQuery(not jQuery UI's draggable). 我用jQuery(而不是jQuery UI的可拖动对象)制作了可拖动div。 This is the code: 这是代码:

let target = null;

this.el.on('mousedown', (ev) => {
    target = this.el;
}).on('mouseup', (ev) => {
    target = null;
});

$(document.body).on('mousemove', 'div', (ev) => {
    if(target) {
        this.x = ev.pageX;
        this.y = ev.pageY;

        this.update(); // update css
    }
});

code is quite simple, and it works well. 代码非常简单,并且效果很好。 The problem is when I dragging from the center or wherever, not left top, element always following cursor's coordination, so always left top of the element is placed where I dragged. 问题是,当我从中心拖动时,或者总是从左到右而不是从左上方拖动元素时,总是将元素的左上方放在拖动的位置。 Yes, it is correct, because that is what I did, but I want to move element from where I dragging. 是的,这是正确的,因为那是我所做的,但是我想将元素从拖动的位置移开。

It is hard to explain, so I'll give more easy one. 很难解释,所以我给一个简单的例子。 If there is a box(div) in (0,0) and it has 5x5 size, and move the box by clicking (0,0) to (10,10), it's destination should be (10,10). 如果(0,0)中有一个box(div)并且它的大小为5x5,然后通过单击(0,0)到(10,10)来移动该box,则它的目的地应该是(10,10)。 But if I move the box by dragging (2.5,2.5) to (10,10), destination must be (12.5,12.5), not (10,10). 但是,如果我通过将(2.5,2.5)拖动到(10,10)来移动框,则目的地必须是(12.5,12.5),而不是(10,10)。

But in my code, destination is (10,10), not (12.5,12.5) because offset from the clicking point is not applied in translation. 但是在我的代码中,目标是(10,10),而不是(12.5,12.5),因为与点击点的偏移量未应用到翻译中。

If you still don't understand what is my problem, please see jquery-ui's draggable example . 如果您仍然不明白我的问题是什么,请参阅jquery-ui的可拖动示例 That is what I want to make exactly. 那就是我想做的。

I know what is the problem, but all of my tried won't work, so I need little help here. 我知道问题出在哪里,但是我尝试的所有方法都无法正常工作,因此在这里我几乎不需要帮助。 Any advice will be very appreciated! 任何建议将不胜感激!

Example is here ! 例子在这里

Just include the offsetX and offsetY values from the mousedown event into your calculation, by subtracting it from the pageX and pageY in the mousemove handler. 只需将mousedown事件中的offsetXoffsetY值包括在您的计算中, pageYmousemove处理程序中的pageXpageY中减去它们pageX

 $(document).ready(function() { function DraggableElement() { this.x = 0; this.y = 0; this.width = 100; this.height = 100; this.el = $('<div></div>') .addClass('box') .appendTo($('#playground')); this.update(); var self = this; var target = null; var offsetX = 0; var offsetY = 0; this.el.on('mousedown', function(ev) { offsetX = ev.offsetX; offsetY = ev.offsetY; target = self.el; }).on('mouseup', function() { target = null; }); $(document.body).on('mousemove', 'div', function(ev) { if(target) { self.x = ev.pageX - offsetX; self.y = ev.pageY - offsetY; self.update(); } }); } DraggableElement.prototype.update = function() { this.el.css({ top: this.y + 'px', left: this.x + 'px', width: this.width + 'px', height: this.height + 'px' }) }; var el = new DraggableElement(); }); 
 * { padding: 0; margin: 0; } #playground { background-color: #eee; position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .box { position: absolute; background-color: red; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="playground"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM