How to set the max drag on jquery-ui - jquery-ui

I'm creating a side menu in the phonegap, I need to drag to the left with a -400px wide limit, and to the right with a 400px wide limit, the problem is that I only know to do drag between parent and without limits:
$( "#draggable" ).draggable({ containment: "parent" });
or
$( "#draggable" ).draggable({ axis: "x" });

You can use Array for defining a bounding box in containment like below:
$("#draggable").draggable({
cursor: 'move',
//containment: "parent"
containment: [-400,0,400,$(document).scrollTop()+$(window).height()]
});
Online Demo (fiddle)

Related

jQuery UI Draggable & Position simultaneously

I want to use jQuery UI Draggable Module and Position Widget simultaneously to create Responsive content.
However, I could not find any usage examples. I've created a simple demo => jsfiddle.net/1ecg2jnc
The problem is; How do I update the offset of the elements when I drag.
<div class="element"
data-x="right"
data-y="top"
data-offset-x="-25"
data-offset-y="+45" >Draggable Element</div>
You can attach a listener to the drag event which updates your offsets. Something like this:
$(this).draggable({
containment: "#zone",
cursor: 'move',
drag: function(event, ui) {
$(event.target).data('offset-x', "+" + ui.offset.left);
$(event.target).data('offset-y', "+" + ui.offset.top);
}
});

Resizeable boxes with constrain

I have multiple boxes in a row and I have added option to resize these boxes using jQuery UI.
You can find the code on JsFiddle: http://jsfiddle.net/alokjain_lucky/ZbTem/
When I resize one box it is pushing the next one to create space. How can I restrict the boxes to push the next box out from the parent?
My jQuery Code:
$(function() {
$( ".resizeable" ).resizable({
containment: "parent",
handles: "e, w",
});
$( ".resizeable" ).resizable( "option", "alsoResize", $(this).siblings().not(this) );
});

Draggable element hidden outside container

Using jQuery UI, I am trying to create an interface with two scrollable containers, each containing many draggable elements. The user can drag an element from one container to the other.
The dropping feature is not an issue. When dropped, the element is detached and recreated in the right place under its new parent.
My problem is that the draggable element cannot be displayed outside its container when the container has position:relative; applied, so while dragging, the element will disappear when it is moved outside the container boundaries.
This default behaviour makes sense, as normally the user would want to drag an element inside its container. As a workaround I had assumed the solution would be to use the draggable property 'appendTo', which I thought would move the element outside its container, but unfortunately this hasn't seemed to have had any effect.
DOM: (each view is scrollable and each container has position:relative and is as large as it needs to be to hold all elements)
BODY
VIEW 1
CONTAINER
DRAGGABLE ELEMENTS
VIEW 2
CONTAINER
DRAGGABLE ELEMENTS
Javascript:
$('div.card').draggable({
appendTo: 'body',
scroll: false //stops scrolling container when moved outside boundaries
});
Please see JSFiddle for a simplified explanation of the problem. I didn't want to bloat the example with droppable code, so this just illustrates the dragging issue. http://jsfiddle.net/Em7Ak/
Many thanks in advance.
I'm not sure if this will fix your exact problem, but I came across this question looking for the same answer and this is what I found.
In the options for .draggable(), pass in helper:'clone' to make a clone of the item automatically so that it can be dragged out of the container. And use appendTo:'body' to put it at the end of the <body> tag. So in my case, my options look somewhat like this, adding in revert:'invalid' to cause it to spring back if it isn't dropped somewhere valid:
jQuery(".myselector").draggable({
helper: 'clone',
revert: 'invalid',
appendTo: 'body'
});
use the "clone" helper and hide the item while dragging it and show it again on stop.
$(".removalbe-recom").draggable({
appendTo: "body",
helper: "clone",
revert: "invalid",
cursor: "move",
containment: "document",
zIndex: 10000,
scroll:false,
start: function (event, ui) {
$(this).hide();
},
stop: function (event, ui) {
$(this).show();
}
});
I had similar problem some months ago.
My need was to be able to use the auto scrolling of one big container from others
Here is my question for more details, JqueryUI, drag elements into cells of a scrolling dropable div containing large table
I found a workaround. The idea is to append the element clone to the scrollable container during the helper construction callback, then append the helper to the body using a setTimeout function after 1ms. The helper position must be mapped on the mouse position to avoid offset problem.
Here is my solution (JSFiddle seems to be down now, try it later if no code is displaying in the windows) : http://jsfiddle.net/QvRjL/134/
$('[id^="drag-"]').each(function() {
$(this).draggable({
opacity: 0.7,
cursorAt: { top: 15, left: 50 },
appendTo: 'body',
containment: 'body',
scroll: true,
helper: function(){
//Hack to append the cartridge to the body (visible above others divs),
//but still belonging to the scrollable container
$('#container').append('<div id="clone" class="cartridge">' + $(this).html() + '</div>');
$("#clone").hide();
setTimeout(function(){
$('#clone').appendTo('body');
$("#clone").show();
},1);
return $("#clone");
}
});
});​
Add position:absolute to card style:
div.card {
position:absolute;
width:100px; height:50px;
border:1px black solid;
background-color:orange;
text-align:center; vertical-align:middle;
}

How to use jqueryui droppable with mouseover

On a web page, I want to display a small div with text in it that is cut off and displayed with ellipses and when there is a mouse-over it should expand to a larger size such that all the text is visible. I then want to be able to use jqueryui drag and drop to move the text box onto a grid. When it is being dragged and placed, I want it to be the small size again.
I have attempted to do this like so:
Mouseover function:
$(function() {
$( ".ui-widget-content" ).mouseover(function(){
$(this).css("width", "200px");
$(this).css("height", "200px");
}).mouseout(function(){
$(this).css("width", "100px");
$(this).css("height", "40px");
})
})
Draggable function:
$(function() {
$( ".ui-widget-content" ).draggable({ snap: "td.chart:not(:first-child)", snapMode: "inner", revert: "invalid", snapTolerance: 40, stack: ".ui-widget-content",
drag: function( event, ui ) {
$(this).css("width", "100px");
$(this).css("height", "40px");
}
});
});
Droppable function:
$(function() {
$( "td.chart:not(:first-child)" ).droppable({
accept: ".ui-widget-content",
drop: function( event, ui ) {
$("#" + ui.draggable.attr("id") ).css("background-color","#D7F970");
}
});
});
Div that is getting dragged and dropped:
Lorem Ipsum
I thought that by using the above, when someone went to drag the div, first the mouse over would be triggered, enlargening the div, then the dragstart would shrink it and it would be fine. However, I am placing it on a table (see selector "td.chart:not(:first-child)" in droppable function), and while it works for the first three rows of the table, the div no longer snaps and gets dropped on the fourth and fifth rows. This problem does not occur if I remove the mouse-over function. Anyone understand what is going on here?
It would maybe help to take a look at your html.
It's just a hunch but maybe it's only a matter of the size of your draggable being too big and being over more than one droppable item.
I would advise trying to change the tolerance of your droppable element to see if it helps :
$( ".selector" ).droppable({ tolerance: "pointer" });
jQuery Documentation states :
Tolerance specifies which mode to use for testing whether a draggable is 'over' a droppable. Possible values:
fit: draggable overlaps the droppable entirely
intersect: draggable overlaps the droppable at least 50%
pointer: mouse pointer overlaps the droppable
touch: draggable overlaps the droppable any amount

Connect draggable to sortable only if visible?

In the jquery ui docs for draggable, we can specify a sortable to connect to:
$( ".selector" ).draggable({ connectToSortable: 'ul#myList' });
is it possible to connect to a sortable only when its visibility is set to 'visible'? Something like:
$( ".selector" ).draggable({ connectToSortable: 'ul#myList:visibility=visible' });
I don't want to let the user drop elements on my sortable target unless it is visible,
Thanks
$( ".selector" ).draggable({ connectToSortable: '#myList:visible' });
Give it a shot and see if it works. :)

Resources