getBoundingClientRect 返回 width、height和下图中的6个属性
抓住一个核心点,就是height、width的值:
所以:
在看源码时,经常看到兼容性IE低版本的写法,了解即可,比如看popper.js源码时:
/*** Get bounding client rect of given element* @function* @ignore* @param {HTMLElement} element* @return {Object} client rect*/function getBoundingClientRect(element) {var rect = element.getBoundingClientRect();// whether the IE version is lower than 11var isIE = navigator.userAgent.indexOf("MSIE") != -1;// fix ie document bounding top always 0 bugvar rectTop = isIE && element.tagName === 'HTML'? -element.scrollTop: rect.top;return {left: rect.left,top: rectTop,right: rect.right,bottom: rect.bottom,width: rect.right - rect.left,height: rect.bottom - rectTop};}