Is there any plan to add jQuery methods to retrieve and set the 
coordinates of an element on the page? It seems like a good addition to 
dimensions.js, perhaps. Here's what I currently use:

$.fn.x = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var x = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     x += o.offsetLeft;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = x;
             } else {
                 result = Math.min(result, x);
             }
         } else {
             o.style.left = n + 'px';
         }
     });
     return result;
};

$.fn.y = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var y = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     y += o.offsetTop;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = y;
             } else {
                 result = Math.min(result, y);
             }
         } else {
             o.style.top = n + 'px';
         }
     });
     return result;
};

If there are multiple elements matched, they will all get the same x- or 
y-position. On retreival, they return the minimum position of all the 
elements in the query result.

Perhaps it might be better to have a single method that returns a pair of 
{x, y} values... though there's something elegant about being able to say 
things like:

$('#obj1').x($('#obj2').x() + 20);

Dave

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to