Get bottom and right position of an element

后端 未结 7 877
遇见更好的自我
遇见更好的自我 2020-12-01 00:45

I\'m trying to get the position of an element within the window like so:

var link = $(element);

var offset = link.offset();
var top = offset.top;
var left =         


        
7条回答
  •  忘掉有多难
    2020-12-01 01:08

    Here is a jquery function that returns an object of any class or id on the page

    var elementPosition = function(idClass) {
                var element = $(idClass);
                var offset = element.offset();
    
                return {
                    'top': offset.top,
                    'right': offset.left + element.outerWidth(),
                    'bottom': offset.top + element.outerHeight(),
                    'left': offset.left,
                };
            };
    
    
            console.log(elementPosition('#my-class-or-id'));
    

提交回复
热议问题