JavaScript
Article

jQuery Function Get Max X,Y Coordinates of Element

By Sam Deering

jQuery function to get the coordinates of the furthest space occupied by an element (or group of elements within the selector). Could be useful if your setting the area of a container based on the absolute positioning of child elements that need to be within the container (ie for drag and drop elements).

max-location-of-element

jQuery.fn.getMaxOccupiedLocation = function()
{
    var maxX = 0, maxY = 0, tmpX, tmpY, elem;
    this.each( function(i,v)
    {
        elem = $(this),
        tmpX = elem .offset().left + elem.width(),
        maxX = (tmpX > maxX) ? tmpX : maxX,
        tmpY = elem .offset().top + elem.height(),
        maxY = (tmpY > maxY) ? tmpY : maxY;
    });
    // console.log(maxX+','+maxY);
    return { x:maxX, y:maxY }; //not the best implementation as it breaks the chain
};

No Reader comments

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.