Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 15:56:02
Original
1317 people have browsed it

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;
Copy after login

Get relative (parent element) position:

var X = $('#DivID').position().top; 
var Y = $('#DivID').position().left; 
var pleft = $("selector").scrollLeft();//元素相对于滚动条左边的偏移量
var pTop = $("selector").scrollTop();//元素相对于滚动条顶部的偏移量

Copy after login

I hope this article will be helpful to everyone’s jQuery programming.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template