By Sam Deering

jQuery Function Get Max X,Y Coordinates of Element

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

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).



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
Login or Create Account to Comment
Login Create Account
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?