Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery-UI Draggable: Print out the coordinate relative to a DIV container

This demo shows how send event when dragging the component around using jQuery. I have a component inside a DIV, and when I drag that component I want to print out the coordinate of the component relative to the DIV container, can any jQuery pro help me out here. Here is what I got so far.

<div id="container" style="width: 400px; height: 4000px; border: 1px solid black;" >
    <div id="draggable" style="width: 150px; height: 150px; background-color: black; cursor: move;">
        <div class="count"/>               
    </div>
</div>  
<script>            
    jQuery(function(){                                
        jQuery("#draggable").draggable({
            containment: "#contain",
            scroll: false,
            drag: function(){

            }
        });
        function updateCoordinate(newCoordinate){
            jQuery(".count").text(newCoordinate);
        }
    });
</script>

In the callback event for drag, I need to figure out the pageX, pageY as well as the offsetX, offsetY to find out the relative position of the component when i drag. I am very new to jQuery. I know that I can obtain the both pageX, pageY and offsetX, offsetY like this

jQuery("#container").click(function(event){
    var offset = jQuery(this).offset();
    var pageX = event.pageX;
    var pageY = event.pageY;
});

but I am not sure how to combine them together.

like image 781
Thang Pham Avatar asked May 02 '11 19:05

Thang Pham


People also ask

How does jQuery ui draggable work?

Using jQuery UI, we can make the DOM(Document Object Model) elements to drag anywhere within the view port. This can be done by clicking on the draggable object by mouse and dragging it anywhere within the view port. If the value of this option is set to false, it will prevent the DOM elements to be dragged .

Why draggable is not working?

Check whether your draggable object is already loaded in the viewport. If it is not, it won't work properly. JUST AFTER the draggable object to be absolutely sure that everything is loaded at the correct time. When you'll be sure everything is ok, then you'll be able to refactor.

What is draggable in JS?

Draggable is a modular drag & drop library, allowing you to start small and build up with the features you need. At its most basic, Draggable gives you drag & drop functionality, fast DOM reordering, accessible markup, and a bundle of events to grab on to.


2 Answers

Like this?

http://jsfiddle.net/aasFx/36/

$(function() {
    $("#draggable").draggable({
        containment: "#contain",
        scroll: false,
        drag: function() {
            var $this = $(this);
            var thisPos = $this.position();
            var parentPos = $this.parent().position();

            var x = thisPos.left - parentPos.left;
            var y = thisPos.top - parentPos.top;

            $this.text(x + ", " + y);
        }
    });
});​
like image 153
James Montagne Avatar answered Nov 28 '22 02:11

James Montagne


What you might want to do is set the parent container CSS to 'position: relative', and the 'draggable item' to 'position: absolute'. Then, using things like $(this).position().left / top will be relative to the parent container.

See here: http://jsfiddle.net/bTULc/1/

like image 22
drowe Avatar answered Nov 28 '22 01:11

drowe