Home >Web Front-end >JS Tutorial >How does jquery get the position of an element in the browser? What are the methods?

How does jquery get the position of an element in the browser? What are the methods?

云罗郡主
云罗郡主forward
2018-10-29 15:36:193110browse

How does jquery get the position of an element in the browser? I believe that many friends who have just come into contact with jquery will have such questions. This chapter will introduce to you how jquery obtains the position of an element in the browser? What are the methods? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Get the object (custom adjustment to open a new window reference element)

var obj = $(”#oButton”);

In the example, the object I get is the pop-up window button, so that the new window created will be based on the position of the button Make adjustments so that the new window created in the instance displays a button in the lower right corner.

2. Get the position of the object element (offset() method)

var offset = obj.offset();

Get the position of the object element, which are the top and left of the element. The calling methods are: offset.left and offset. top, you can know the left and top positions of the current object.

3. Get the width of the object element (width() method)

var right = offset.left+obj.width();

In the example, the position of the lower right corner of the object is obtained and the left position of the new window is created.

4. Get the height of the object element (height() method)

var down = offset.top+obj.height();

In the example, the position of the lower right corner of the object is obtained and the top position of the new window is created.

5. Define CSS styles and set the left and top respectively.

‘left’:right,
‘top’:down,

Pay attention to the values ​​of margin and padding in CSS styles, as well as the compatibility of browsers IE and Firefox.

Combined with the above example, it is very simple for jQuery to obtain the element position, height and width. Through the built-in methods offset(), width() and height(), we can realize our dynamic adjustment and open new window function. For other extended applications The same is true, understanding jQuery's method of obtaining element position, height and width will be very practical for creating new elements and adjusting element position and size.

The above is a complete introduction to how jquery gets the position of an element in the browser? What are the methods? If you want to know more about JavaScript video tutorial, Please pay attention to the PHP Chinese website.


The above is the detailed content of How does jquery get the position of an element in the browser? What are the methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lvyestudy.com. If there is any infringement, please contact admin@php.cn delete