Home > Web Front-end > JS Tutorial > jQuery get coordinates of element

jQuery get coordinates of element

尊渡假赌尊渡假赌尊渡假赌
Release: 2025-02-27 09:03:11
Original
873 people have browsed it

jQuery get coordinates of element

How to use jQuery to get element coordinates

jQuery provides a variety of ways to obtain coordinate information of elements.

1. .position() Method: Get the coordinates of the element offsetting the parent element relative to its . Offset parent element refers to the nearest ancestor element with a non-static positioning method.

var pos = $('#wrapper').position();
console.dir(pos);
// 输出:left: 0, top: 20
Copy after login

2. .offset() Method: Get the coordinates of an element relative to the document.

var offset = $('#wrapper').offset();
console.dir(offset);
// 输出:left: 70, top: 40
Copy after login

3. Get the coordinates directly: Combined with the method, the .offset() and left attributes are directly extracted. top

var elem = $("#wrapper");
var x = elem.offset().left;
var y = elem.offset().top;
console.log('x: ' + x + ' y: ' + y);
// 输出:x: 70 y: 40
Copy after login

4. Custom Function:getCoord() Encapsulate a custom function to make the code more concise and easy to read.

jQuery.fn.getCoord = function() {
  var elem = $(this);
  var x = elem.offset().left;
  var y = elem.offset().top;
  console.log('x: ' + x + ' y: ' + y);
  // 输出:x: 70 y: 40
  return {
    "x": x,
    "y": y
  };
};

$('#wrapper').getCoord();
// 输出:Object { x: 70, y: 40 }
Copy after login

Note: The function is not efficient because it interrupts jQuery chain operations. getCoord()

Frequently Asked Questions about jQuery Coordinates and Elements (FAQ)

Q1: How to use jQuery to get the coordinates of an element relative to a document?

Use the

method. This method returns an object containing .offset() and top properties, respectively, representing the vertical and horizontal positions of an element relative to the document. left

var position = $("#element").offset();
console.log("元素位于: " + position.left + ", " + position.top);
Copy after login

Q2: What is the difference between and .position() methods in jQuery? .offset()

Returns the coordinates of an element relative to its offset parent element; .position() Returns the coordinates of an element relative to the document. .offset()

Q3: How to set the coordinates of an element using jQuery?

Use the

method to combine an object containing the .offset() and top attributes. left

$("#element").offset({ top: 100, left: 200 });
Copy after login

Q4: How to use jQuery to get the absolute coordinates of an element?

Use the

method to get the absolute coordinates of the element (relative to the document). .offset()

Q5: How to use jQuery to get the coordinates of an element relative to its parent element?

Use the

method. .position()

The above is the detailed content of jQuery get coordinates of element. For more information, please follow other related articles on the PHP Chinese website!

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