简体   繁体   English

jQuery UI Draggable-光标不与Draggable元素保持一致

[英]Jquery UI Draggable - Cursor not staying with draggable element

I'm trying to create a popup that has the Jquery UI draggable() functionality. 我正在尝试创建一个具有Jquery UI draggable()功能的弹出窗口。 I've gotten it to work, but the cursor does not stay with the popup element. 我已经开始使用它了,但是光标并没有停留在popup元素上。 When you click on the popup (the div that was made draggable), if you drag it to the left, the mouse cursor goes past the div element and does not successfully drag the div element all the way to the left of the page. 当您单击弹出窗口(使div成为可拖动的div)时,如果将其拖动到左侧,则鼠标光标会越过div元素,并且不会成功地将div元素一直拖动到页面的左侧。

To see what I'm talking about see this jsfiddle http://jsfiddle.net/lennox02/LvmSG/ 要了解我在说什么,请参阅此jsfiddle http://jsfiddle.net/lennox02/LvmSG/

And Here's the code: 这是代码:

document.getElementById("clicked").onclick = function iframe_open() {


var div = document.createElement('div');
div.style.position = 'absolute';
div.style.backgroundColor = 'rgba(0,0,0,0.1)';
div.style.minHeight = '100%';
div.style.minWidth = '100%';
div.style.top = '0px';
div.style.left = '0px';
div.id = 'overlay';
div.style.zIndex = '10000000';
document.body.appendChild(div);

var z = document.createElement('div');
z.style.position = 'absolute';
z.style.top = '150px';
z.style.marginLeft = 'auto';
z.style.marginRight = 'auto';
z.style.left = '0';
z.style.right = '0';
z.style.backgroundColor = 'black';
z.style.width = '325px';
z.style.height = '175px';
z.id = 'popup';
z.style.zIndex = '10000001';
document.body.appendChild(z);



$( "#popup" ).draggable();

$("#overlay").click(function() {

    var elem = document.getElementById("overlay");

    elem.parentNode.removeChild(elem);

    var elem2 = document.getElementById("popup");

    elem2.parentNode.removeChild(elem2);

});

}

I've tried using the cursorAt:{} option, but although it shifts where the element is when it's initial clicked, it doesn't fix the cursor overshooting the element when the element is dragged. 我试过使用cursorAt:{}选项,但是尽管它在最初单击时会移动元素的位置,但是并不能解决在拖动元素时使光标超出元素的问题。

You can set the elements left position so that it's centered by getting the windows width divided by two then minus half the width of the element. 您可以将元素的左侧位置设置为居中,方法是将窗口宽度除以2,然后减去元素宽度的一半。

Like this 像这样

z.style.left = (($(window).width() - $(z).outerWidth()) / 2) + "px";

here's your fiddle with the above code 这是上面代码的小提琴

Or you could remove the margins in the start event of the draggable function. 或者,您可以在可拖动功能的开始事件中删除边距。

Like this 像这样

$("#popup").draggable({
    start: function( event, ui ) {   
        $(this).css('margin-right', '');
        $(this).css('margin-left', '');
    }
});

here's your fiddle with this code 这是您使用此代码的小提琴

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

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