简体   繁体   English

滚动容器时元素拖动出错

[英]Dragging of element goes wrong when the container is scrolled

In this Fiddle , I made the red container draggable. 在这个小提琴中 ,我使红色容器可拖动。 As you can see, this is working fine but when the green container ( parent container ) is scrolled a bit and then the red container is dragged, the dragging is not happening at correct position. 如您所见,这工作正常,但是当绿色容器( 父容器 )稍微滚动一下,然后拖动红色容器时,拖动不会在正确的位置进行。

Can someone please tell me what could be the problem? 有人可以告诉我可能是什么问题吗?

I tried e.pageX , e.clientX and e.offsetX but still couldn't able to fix this. 我尝试了e.pageXe.clientXe.offsetX但仍然无法解决此问题。 ( or maybe I missed something ) 或者我错过了什么

You need to add the scrolling top position to your shape. 您需要将滚动顶部位置添加到形状中。 If you want your square stay on the correct position check that: 如果要让正方形停留在正确的位置,请检查:

function repositionShape(e) {
    $self = repositionStart.self;
    $commentSection = repositionStart.commentSection;

    var dy = $('.wrapper').scrollTop(); // Get wrapper scroll position

    $self.css({
    'left': e.clientX - repositionStart.offset.left,
        'top': e.clientY - repositionStart.offset.top +dy // Add delta to your square position
    });
}

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

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