views:

3893

answers:

1

hey well i am working on something that which will basically create a business card online here is a example.

http://weblayoutsrus.com/draggable/example.html

im basically trying to layout the div tags like fullname ect with css, then i want to grab the X & Y co-ordinates of the div tags relative to there container so that it can b passed on to PHP GD library so that the image can be made.

I tried using the position(): function in jquery but its giving the relative position.

If i cant do it soon il have to pay someone to do it, does anyone have any pointers that may help ?

+1  A: 

Check out the jQuery dimensions plugin to find the correct position of an element.

EDIT: As mentioned in the comments, the dimensions plugin was merged into the core. Documentation can be found here: offset and here position.

The linked example could be extended like this: Replace the following code

var x = $("#Company_Name").position().left;
var y = $("#Company_Name").position().top;

$("p:last").text( "Full Name: left: " + x + ", top: " + y );

with this:

var $companyname = $("#Company_Name");
var x = $companyname.position().left;
var y = $companyname.position().top;
var ax = $companyname.offset().left;
var ay = $companyname.offset().top;

$("p:last").text( "Full Name: left: " + x + "/" + ax + ", top: " + y + "/" + ay);

notice, I searched the element only once and stuffed it into a variable. That's a good idea if the search can take some time.

EDIT2: I just realized, that this does not work as I expected it. I wrote some function to calculate the relative position:

function toString(obj){
     return "{top: " + obj.top + " left: " + obj.left + "}";
    }

    function getRelativePosition(selector){

       var $parentElem = $("#parentElem");
       var $element = $(selector);

       var elementPosition = $element.position();
       var parentPosition = $parentElem.position();

       return {top: elementPosition.top - parentPosition.top, 
        left: elementPosition.left - parentPosition.left};
    }

  $(document).ready(function(){

       var position = $("#Company_Name").position();
     var relativePosition = getRelativePosition("#Company_Name");

     $("p:last").text("position: " + toString(position) + " relativePosition: " + toString(relativePosition));
    });

Let me know if this works for you. And someone may tell me what the difference between position and offset in jQuery :-\

EDIT3: To get the position of more then one element, you could call the functions with different ids or give them all the same name or the same class to have something to select them all. Here is a working example:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

<script type="text/javascript">

function toString(obj){
 return "{top: " + obj.top + " left: " + obj.left + "}";
}

function getRelativePosition(selector){

   var $parentElem = $("#parentElem");
   var $element = $(selector);

   var elementPosition = $element.position();
   var parentPosition = $parentElem.position();

   return {top: elementPosition.top - parentPosition.top, 
    left: elementPosition.left - parentPosition.left};
}

$(document).ready(function(){

$(".cardfield").each(function(i){

 var position = $(this).position();
 var relativePosition = getRelativePosition(this);

 $("p:last").append("element: " + this.id + " position: " + toString(position) + " relativePosition: " + toString(relativePosition) + "<br>");

});
});

</script>
</head>
<body>

<br><br><br><br><br>

<div id="parentElem" style="margin: 0px; top:10px; background-color: #ddd;">
<br><br>
<div class="cardfield" id="Company_Name1">Foo Bar</div>
<br>
<div class="cardfield" id="Company_Name2">Company Name</div>
<div class="cardfield" id="firstname" style="float: right;">Tim</div><br/>
<div class="cardfield" id="lastname" style="float: right;">B&uuml;the</div>
<br><br>
</div>

</p></p>
</body>
</html>

Note: I gave all fields the class "cardfield" and used the jQuery to select them. Then I used the each function to work with the elements. Inside the function I gave to each, "this" refers to the current object.

Tim Büthe
that plugin has been part of jquery core since 1.3 (i think)
mkoryak
I read this also, but they only took the most popular or functions or something, right? Could you provide a pointer?
Tim Büthe
Hey yeah this plugin has been mentioned before does anyone know and good documentation on it that can help me use it for a tiny example of code on how to show the position of the div tag relative to container ?
hey thanks for your reply but atm it's still not getting me the position relative but i'll keep on trying though. i was wondering if you've got any spare time on your hands so that you could possibly get this working for me. Willing to pay and i know its not going to be cheap and that's fine !
hey got the code working not but offer still stands if u would like some work :)
Thanks for the offer, but I have no free time at all. Have you tried my "getRelativePosition" function?
Tim Büthe
hey i was wondering how would i edit this so that it will show the position of multiple elements ?
@Tim Büthe -- Hey ! Thanks ! This saved my lot of time ! :)
KutePHP