🤯 50% Off! 700+ courses, assessments, and books

jQuery Function Get Max X,Y Coordinates of Element

Sam Deering
Share

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
};