Gets the width and height of the DOM element

Source: Internet
Author: User

First, get the size of CSS

1. First type via inline style

    var box = document.getElementById ('box');     var w = box.style.width;     var h = box.style.height;

2. By calculating the size of the element ( but in the case of IE, there is a problem, then do not write widht and height of the CSS to return to Auto);

    var style = window.getComputedStyle? window.getComputedStyle (box,nullnull | | Box.currentstyle;     var w = style.width;     var h = style.height;

3. Get the element size by the Cssrules ( or Rules) property in the Cssstylesheet object (but cannot get the calculated style )

    var sheet = document.stylesheets[0];     var rule = (Sheet.cssrules | | sheet.rules) [0];     var w = rule.style.width;     var h = rule.style.height;

None of the above three methods are possible.

Second, get the actual size

1.clientWidth and ClientHeight

    var w = box.clientwidth;     var h = box.clientheight;

Description: Padding and scroll changes, only change

2.scrollWidth and Box.scrollheight;

    var w = box.scrollwidth;     var h = box.scrollheight;

Description, 1) border changes, different browsers have different changes 2) padding change, change 3) margin change, no change

3.offsetWidth and Offsetheight

    var w = box.scrollwidth;     var h = box.scrollheight;

shows that padding and border are changed.

Third, get the element of the change of the distance (originally only from the left and top)

1.clientLeft and ClientTop
This set of properties lets you get the size of the left and top borders of the element.

    var l = box.clientleft;     var t = box.clienttop;

2. Get the position of the relative parent element

    var l = box.offsetleft;     var t = box.offsettop;     var parent = box.offsetparent;  // gets the ambush element, returns the body

Description, if there is no position:absolute, if each browser has a different explanation

Then get the elements in the multilayer to the body or HTML distance, the code is as follows:

    function Offsetleft (element) {        var left = element.offsetleft;         var parent = element.offsetparent;                  while NULL ) {            + = parent.offsetleft            ; = parent.offsetparent;        }         return Left ;    }

3.

// get the position above the scrolling content // get the left position of scrolling content

Gets the width and height of the DOM element

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.