Js obtains the implementation code of the element relative to the window location. js window

Source: Internet
Author: User

Js obtains the implementation code of the element relative to the window location. js window

JS obtains the offsetTop, offsetLeft, and other attributes of an element.

Obj. clientWidth // obtain the element width.

Obj. clientHeight // height of the element
Obj. offsetLeft // left of the element relative to the parent Element
Obj. offsetTop // The top of the element relative to the parent Element
Obj. offsetWidth // The width of the element
Obj. offsetHeight // The height of the element

Differences:

ClientWidth = width + padding
ClientHeight = height + padding
OffsetWidth = width + padding + border
OffsetHeight = width + padding + border
Offset has the border Width higher than the client.

// Obtain the ordinate of an element (relative to the window) function getTop (e) {var offset = e. offsetTop; if (e. offsetParent! = Null) offset + = getTop (e. offsetParent); return offset;} // obtain the abscissa (relative to the window) of the element. function getLeft (e) {var offset = e. offsetLeft; if (e. offsetParent! = Null) offset + = getLeft (e. offsetParent); return offset ;}

I have also written a JS article about getting element location: JS gets the offsetTop and offsetLeft attributes of an element. We can use the offsetTop and offsetLeft attributes to get the position of the element relative to the window, however, the offsetTop and offsetLeft attributes are located relative to the parent element, and the elements that need to be obtained are not in the outermost layer. Therefore, traversing the offset attribute of the parent element is indispensable. Efficiency becomes a problem.

// Obtain the ordinate of an element (relative to the window) function getTop (e) {var offset = e. offsetTop; if (e. offsetParent! = Null) offset + = getTop (e. offsetParent); return offset;} // obtain the abscissa (relative to the window) of the element. function getLeft (e) {var offset = e. offsetLeft; if (e. offsetParent! = Null) offset + = getLeft (e. offsetParent); return offset ;}

Fortunately, the browser provided me with the corresponding interface getBoundingClientRect. This method was first available in IE. Later, the browser also supported this method, and it is more complete, IE can only obtain the left, top, bottom, and right attributes of the element, while the modern browser can also obtain the width and

Chrome Firefox (Gecko) Internet Explorer Opera Safari
1.0 3.0 (1.9) 4.0 (Yes) 4.0

It should be noted that bottom is the distance between the bottom of the element and the top of the window, rather than the position bottom in css relative to the bottom of the window. Likewise, the rihgt attribute is the distance from the rightmost side of the element to the left side of the window.

var box = document.getElementById("box");var pos = box.getBoundingClientRect();box.innerHTML = "top:"+pos.top +  "left:"+pos.left +  "bottom:"+pos.bottom +  "right:"+pos.right +  "width:"+pos.width +  "height:"+pos.height

Original article, reprinted Please note: Reprinted from front-end development


How does JS determine the absolute position of an element relative to the parent window?

<Div id = "div_status" style = "width: 101px; height: 102px; border: 1px solid # ffff00; position: fixed; top: 103px; left: pixel PX; "> xianshineirong </div>
<Script type = "text/javascript">
Window. onload = function (){
Window. alert (document. getElementById ('div _ status'). style. left );
}
</Script>

How to find JS Code corresponding to webpage elements ()

Where is the start () function body?
If so, you can use ctrl + F on this page to find whether this function is available;
If not, find the I hope it will help you

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.