JQuery Dragging a hidden window?

I'm trying to hide my draggable item, so it cannot be dragged outside the visible window, which works well if the user is at the top of the page, however, if you scroll down at all, it will ruin everything.

How can i do this?

$(".chat-wrapper > li.draggable").draggable({ greedy: true, handle: '.chat-button', containment: 'html' }); 
+4
source share
4 answers

Just use containment: 'window' and possible scroll: false

Example:

 $('#selector').draggable({ containment: 'window', scroll: false }); 

Additional Information:

containment , scroll

+5
source
 $(".chat-wrapper > li.draggable") .on('mousemove',function(){ // Update containment each time it dragged $(this).draggable({ greedy: true, handle: '.chat-button', containment: // Set containment to current viewport [$(document).scrollLeft(), $(document).scrollTop(), $(document).scrollLeft()+$(window).width()-$(this).outerWidth(), $(document).scrollTop()+$(window).height()-$(this).outerHeight()] }); }) .draggable({ scroll: false }); 
+2
source

Only one mistake for my situation: I have a draggable DIV with negative top and left margins (to fit in the middle with asbolute position). Thus, containment works for a positive value of X and Y, but the DIV also goes into negative coordinates, and this should not. Is there a workaround?

0
source

try removing greedy:true

I tried to achieve the same and remove it.

0
source

Source: https://habr.com/ru/post/1397304/


All Articles