简体   繁体   English

拖动时设置jquery可拖动元素的位置

[英]Set position of jquery draggable element while dragging

In my web app, there is a draggable element.在我的网络应用程序中,有一个可拖动元素。 I need to set the left position of this element when the element reaches a certain limit while dragging.当元素在拖动时达到某个限制时,我需要设置该元素的左侧位置。 Using jQuery draggable widget, I have access to the position of the element:使用 jQuery 可拖动小部件,我可以访问元素的位置:

function drag(e, ui) {
 console.log(ui.position.left);
}

Let say my left attribute is setted to 1100px, I need to set it to 500px and this, without stopping the dragging.假设我的左属性设置为 1100 像素,我需要将其设置为 500 像素,而不会停止拖动。

I have three functions: dragStart, drag, and gradEnd.我有三个函数:dragStart、drag 和 gradEnd。 Currently, I managed to get only one result: when setting ui.position.left = 500;目前,我只得到了一个结果:设置ui.position.left = 500; on the drag function (using a condition), the left position is set to 500 but of course, the element is then stuck at 500px.在拖动功能上(使用条件),左侧位置设置为 500,但当然,该元素然后停留在 500px。 The reason is that every time the drag function is triggered, the left position is setted to 500.原因是每次触发拖动功能时,左位置设置为500。

If the code runs only once the line ui.position.left = 500;如果代码只运行一次ui.position.left = 500; the position left attribute is set to 500, but directly reset to 1100. position left 属性设置为500,但直接重置为1100。

How can I set the left attribute once and for all?如何一劳永逸地设置左属性?

 $("#divId").draggable({ drag: drag, }) function drag(e, ui) { if (ui.position.top > 50) { ui.position.left = 100; } }
 #divId { height: 70px; background-color: white; border: 4px solid #000000; text-align: center; color: black; cursor: grab; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div id="divId"> Bubble </div>

I am not sure how jQuery Draggable handles things under the hood, but even after setting ui.position.left = 100 , it does not register in the event until after dragging has stopped - that is why I opted to check the actual CSS property of the element that is being targeted.我不确定jQuery Draggable如何处理ui.position.left = 100事情,但即使在设置ui.position.left = 100 ,它也不会在拖动停止后注册到事件中 - 这就是为什么我选择检查的实际CSS属性被定位的元素。

I have also provided an example (closure/functional based) which demonstrates how to handle this without having to check CSS ..我还提供了一个示例(基于闭包/功能),它演示了如何在无需检查CSS情况下处理此问题。



First example:第一个例子:

 $("#divId").draggable({ drag: drag }); function drag(e, ui) { if (ui.position.top > 50) { $("#container").css('padding-left', '100px'); $(this).css('left', '0px'); } if (ui.position.left < 0) { ui.position.left = 0 } }
 #divId { height: 70px; background-color: white; border: 4px solid #000000; text-align: center; color: black; width: 300px; cursor: grab; } #container { height: 100vh; width: 1000px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div id="container"> <div id="divId"> Bubble </div> </div>




Second example, more of a 'closure based functional approach': does not require you to check CSS ..第二个例子,更多的是“基于闭包的功能方法”:不需要你检查CSS ..

 $("#divId").draggable({ drag: drag() }); function drag(e, ui) { let TRIGGER = false, TOP_THRESHOLD = 50, LEFT_POSITION = 100; return function(e, ui) { if (TRIGGER) { ui.position.left = LEFT_POSITION; } else if (ui.position.top > TOP_THRESHOLD) { TRIGGER = true; ui.position.left = LEFT_POSITION; } } }
 #divId { height: 70px; background-color: white; border: 4px solid #000000; text-align: center; color: black; cursor: grab; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div id="divId"> Bubble </div>



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

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