Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Draggable Constraint

what I am trying to do is have an large image contained within a smaller div that the user can drag around within the containing div (easy enough)... similar to http://oneblackbear.com/draggable/index.html but I want to prevent users from dragging it any further then the container boundary. With the above example the user can drag the image completely outside of the containing div... I want to prevent the user from ever dragging the image outside of the parent div at all.

I have tried using jQuery UI draggable but the problem is if I use the constraint option as soon as you drag the image it locks to the bottom right and is no longer draggable because the child element is larger then the parent constraint. I am not sure if the ui draggable constraint is only intended for smaller objects then the parent container.

Does anyone have any ideas how do do with? preferably with jQuery Draggable?

like image 798
camslaz Avatar asked Mar 11 '11 23:03

camslaz


People also ask

How to use jQuery UI draggable?

In this article, we learnt about the jQuery UI draggable functionality which basically allows the DOM elements to be moved or dragged using the mouse. This is done by clicking on the object to be dragged using mouse and dragging it anywhere within the view port.

How do I make a drag and drop widget in jQuery?

Make the selected elements draggable by mouse. If you want not just drag, but drag & drop, see the jQuery UI Droppable plugin, which provides a drop target for draggables. The draggable widget uses the jQuery UI CSS framework to style its look and feel.

How to set the draggable items to be contained in container?

The containment option is used to set that the draggable items will be contained in the specified container. Syntax: The containment option takes an element as string, jquery, element and is initialized as follows: $ (".drag").draggable ( { containment: "#gfg_container", }); var containmentOpt = $ (".drag") .draggable ( "option", "containment");

How do I style a draggable element using CSS?

If draggable specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes option: ui-draggable: The draggable element. When the draggable is disabled, the ui-draggable-disabled class is added.


2 Answers

var productHeadOffset = jQuery('#productHead').offset();
var productHeadHeight = jQuery('#productHead').height();
var productHeadWidth = jQuery('#productHead').width();
var productHeadImageHeight = jQuery('.productHeadImage').height();

var right = productHeadOffset.left;
var bottom = productHeadOffset.top;

var left = (img.width > productHeadWidth) ? (productHeadWidth + productHeadOffset.left) - img.width : 0;
var top = (img.height > productHeadImageHeight) ? (productHeadHeight + productHeadOffset.left) - img.height : 0;

jQuery('.productHeadImage').draggable({ containment: [left, top, right, bottom] });
like image 73
Brendan Avatar answered Oct 09 '22 17:10

Brendan


This is the solution that worked for me, although I'm still having issues in Chrome when the page is scrolled:

var cropBoundsOffset = $("cropBounds").offset();
var cropBoundsHeight = $("cropBounds").height();
var cropBoundsWidth = $("cropBounds").width();
var imageHeight = $("cropImage").height();
var imageWidth = $("cropImage").width();

var right = cropBoundsOffset.left;
var bottom = cropBoundsOffset.top;
var left = (imageWidth > cropBoundsWidth) ? (cropBoundsWidth + cropBoundsOffset.left) - imageWidth : 0;
var top = (imageHeight > cropBoundsHeight) ? (cropBoundsHeight + cropBoundsOffset.top) - imageHeight : 0;

var border_left = parseInt($("cropBounds").css("border-left-width"));
var border_top = parseInt($("cropBounds").css("border-top-width"));

$("cropImage").draggable("option", "containment",  [
    left + border_left,
    top + border_top,
    right,
    bottom
]);
like image 24
Jonathan Avatar answered Oct 09 '22 15:10

Jonathan