Home > Article > Web Front-end > How to get the absolute and relative position of page elements using jQuery_jquery
The example in this article describes how jQuery obtains the absolute and relative position of page elements. Share it with everyone for your reference. The details are as follows:
To get the absolute X, Y coordinates of an element on the page, you can use the offset() method:
var X = $('#DivID').offset().top; var Y = $('#DivID').offset().left;
Get relative (parent element) position:
var X = $('#DivID').position().top; var Y = $('#DivID').position().left; var pleft = $("selector").scrollLeft();//元素相对于滚动条左边的偏移量 var pTop = $("selector").scrollTop();//元素相对于滚动条顶部的偏移量
I hope this article will be helpful to everyone’s jQuery programming.