Home >Web Front-end >JS Tutorial >How to get the absolute and relative position of page elements using jQuery_jquery

How to get the absolute and relative position of page elements using jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 15:56:021335browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn