Home > Web Front-end > JS Tutorial > JavaScript DOM element size and position_Basic knowledge

JavaScript DOM element size and position_Basic knowledge

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:04:33
Original
1307 people have browsed it

1 Get the CSS size of the element

1. Get the size of the element through style inline

Copy code The code is as follows:

var box = document.getElementById('box'); // Get element;
box.style.width; box.style.height;

// PS: style can only get the width and height in the CSS style of the inline style attribute. If there is one, get it; if there is none, return empty;

2. Obtain the size of the element through calculation

var style = window.getComputedStyle ? window.getComputedStyle(box,null) : null || box.currentStyle;
      style.width;                                         

// PS: Obtain the size of the element through calculation, regardless of whether it is inline/inline or link, it returns the calculated result;
// If the size is set by itself, it will return the size of the element; if it is not set by itself, non-IE will return the default size, and IE will return auto;


3. Get the size of the element through the cssRules (or rules) attribute in the CSSStyleSheet object;


Copy code The code is as follows: var sheet = document.styleSheets[0]; var sheet var rule = (sheet.cssRules || sheet.rules)[0]; // Get the first rule;
Rule.style.width; Rule.style.width; // 200px;


PS: cssRules can only obtain the width and height of inline and link styles, but cannot obtain inline and calculated styles;

Summary: The above three CSS methods of obtaining element size can only obtain the CSS size of the element, but cannot obtain the actual size of the element itself; such as adding padding/scroll bar/border;

2 Get the actual size of the element

1.clientWidth and clientHeight

This set of attributes can obtain the size of the element's visual area, including the space occupied by the element's content and padding;
box.clientWidth; // 200;
PS: The element size is returned, but there is no unit. The default unit is px;
PS: Regarding the actual size of the element, clientWidth and clientHeight are understood as follows:
​ 1. Add a border to the element, no change, 200;
​ 2. Add outer border to element, no change, 200;
3. Add scroll bar, final value = original size - scroll bar size; 184;
4. Increase padding, final value = original size padding size; 220;
PS: If no CSS width and height are set, then non-IE will include the calculated size of the scroll bar and padding; while IE will return 0;

2.scrollWidth and scrollHeight

This set of attributes can obtain the total height of the element content without scroll bars;
box.scrollWidth;
// PS: Returns the element size, the default unit is px; if no CSS width and height are set, it will get the calculated width and height;

3.offsetWidth and offsetHeight

This set of attributes can return the actual size of the element, including borders/padding and scroll bars;
box.offsetWidth; 200
PS: The element size is returned, and the default unit is px; if no CSS width and height are set, it will get the calculated width and height;
PS: Regarding the actual size of the element, the understanding is as follows:
1. Add border, final value = original size border size; 220;
2. Increase padding, final value = original size padding size; 220;
​ 3. Added external border strongholds, no change;
​ 4. Increase the scroll bar, no change, no decrease;

PS: For obtaining the element size, it is generally more convenient to use block-level elements with CSS sizes set;

3 Get the surrounding size of the element

1.clientLeft and clientTop
// This set of attributes can get the size of the left border and top border set by the element;
box.clientLeft;

2.offsetLeft and offsetTop (offset)

// 这组属性可以获取当前元素边框相对于父元素边框的位置;
  box.offsetLeft;                  // 50;
  // PS:获取元素当前相对于父元素的位置,最好将它设置为定位position:absolute;
  // PS:加上边框和内边距不会影响它的位置,但加上外边据会累加;

  box.offsetParent;                 // 得到父元素;
  // PS:offsetParent中,如果本身父元素是<body>,非IE返回body对象,IE返回html对象;
  // 如果两个元素嵌套,如果上级父元素没有使用定位position:absolute,那么offsetParent将返回body或html对象;

// 如果说在很多层次里,外层已经定位,获取任意一个元素距离页面上的位置,可以不停的向上回溯获取累加来实现;
  box.offsetTop+box.offsetParent.offsetTop;     // 只有两层的情况下;
  // 如果多层的话,就必须使用循环或递归;
  function offsetLeft(element){
    var left = element.offsetLeft;        // 得到第一层距离;
    var parent = element.offsetParent;      // 得到第一个父元素;
    while(parent !== null){            // 判断如果还有上一层父元素;
      left += parent.offsetLeft;        // 将得到的距离累加;
      parent = parent.offsetParent;       // 将父元素也回溯;
    }                       // 然后循环;
    return left;                 // 得到最终距离;
  }

Copy after login

3.scrollTop and scrollLeft

// 这组属性可以获取被滚动条隐藏的区域大小,也可设置定位到该区域;
  box.scrollTop;                  // 获取滚动内容上方的位置;

// 设置滚动条滚动到最初始的位置;
  function scrollStart(element){
    if(element.scrollTop != 0){
      element.scrollTop = 0;
    }
  }
Copy after login

Four getBoundingClientRect() method

// 这个方法返回一个矩形对象,包含四个属性:left/top/right和bottom;
// 分别表示元素各边与页面上边和左边的距离;
  var box = document.getElementById('box');
  alert(box.getBoundingClientRect().top);    // 元素上边距离页面上边的距离;
  alert(box.getBoundingClientRect().right);   // 元素右边距离页面左边的距离;
  alert(box.getBoundingClientRect().bottom);   // 元素下边距离页面上边的距离;
  alert(box.getBoundingClientRect().left);    // 元素左边距离页面左边的距离;
  // PS:IE/Firefox/Opera/Chrome/Safari都支持;
  // 但在IE中,默认坐标从(2,2)开始计算,导致最终距离比其他浏览器多出两个像素;
  document.documentElement.clientTop;      // 非IE为0,IE为2;
  document.documentElement.clientLeft;      // 非IE为0,IE为2;
// 兼容getBoundingClientRect()
  function getRect(element){
    var rect = element.getBoundingClientRect();
    var top = document.documentElement.clientTop;
    var left = document.documentElement.clientLeft;
    return {
      top:rect.top-top,           // 元素上边距-页面的上边距(0-0或2-2);
      bottom:rect.bottom-top,
      left:rect.left-left,         // 元素左边距-页面的左边距(0-0或2-2);
      right:rect.right-left
    }
  };
Copy after login

Five Summary

1. Offset dimension: includes all visible space occupied by the element on the screen;

The visible size of an element is determined by its height and width, including padding/scrollbars and borders;
2. Client dimension: refers to the space occupied by the element content and its padding;
3.Scroll size (scroll dimension): The size of the element containing the scrolling content;

Related labels:
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