2

My jquery UI draggable is working great, except when I try to drag something off-screen, causing the screen to scroll. Then the selected item jumps to who knows where - you can't see it anymore. Anyone know how I can troubleshoot this? Thank you.

Right now we are on jquery-ui-1.8.24 and jquery-1.8.2

4

3 回答 3

1

在无法使用 jQuery 找到解决方案后,我实现了自己的拖动 - 对我很有用。它还允许我删除 jQuery UI,这很棒。请注意,draggable 和 handle 是 jquery 元素,而 limits 是一个对象。这是代码 -

function initializeDrag(draggable, handle, limits) {
                            var initialMousePosition, initialDraggablePosition;
                            handle.on('mousedown', function (e) {
                                initialMousePosition = {
                                    x: e.pageX,
                                    y: e.pageY
                                };
                                initialDraggablePosition = {
                                    top: parseInt(draggable.css('top'), 10),
                                    left: parseInt(draggable.css('left'), 10)
                                }
                                document.addEventListener('mousemove', onMouseMove);
                                document.addEventListener('mouseup', onMouseUp);
                            });

                            function onMouseMove(e) {
                                var verticalDelta = e.pageY - initialMousePosition.y;
                                var horizontalDelta = e.pageX - initialMousePosition.x;
                                var topRes = initialDraggablePosition.top + verticalDelta;
                                var leftRes = initialDraggablePosition.left + horizontalDelta;
                                topRes = Math.max(topRes, limits.top);
                                topRes = Math.min(topRes, limits.bottom);
                                leftRes = Math.max(leftRes, limits.left);
                                leftRes = Math.min(leftRes, limits.right);

                                draggable.css({
                                    top: topRes + 'px',
                                    left: leftRes + 'px'
                                });
                            }

                            function onMouseUp() {
                                document.removeEventListener('mousemove', onMouseMove);
                                document.removeEventListener('mouseup', onMouseUp);
                            }
                        }
于 2014-09-24T13:12:51.460 回答
1

请注意,当使用 jQuery-ui 时,您appendTo应该使用的元素样式position: relativedraggable

于 2013-10-25T09:41:58.490 回答
0

http://jsfiddle.net/g4xHG/

如果在body元素上设置溢出属性,这个问题就会消失。不知道为什么。

body {
    overflow: auto;
}
于 2013-07-10T16:48:47.830 回答