其他分享
首页 > 其他分享> > 获取视窗高度和元素高度

获取视窗高度和元素高度

作者:互联网

// 获取视窗高度
export function getClientHeight() {
  var clientHeight = 0;
  if (document.body.clientHeight && document.documentElement.clientHeight) {
    var clientHeight =
      document.body.clientHeight < document.documentElement.clientHeight
        ? document.body.clientHeight
        : document.documentElement.clientHeight;
  } else {
    var clientHeight =
      document.body.clientHeight > document.documentElement.clientHeight
        ? document.body.clientHeight
        : document.documentElement.clientHeight;
  }
  return clientHeight;
}
/**
 * @description: 获取元素高度
 * @param {*} className 元素类名
 * @return {*}
 * @author: 
 */
export function getDomClientHeight(className) {
  const dom = document.getElementsByClassName(className);
  let domHeight = 0;
 // 此为业务代码,若类名唯一可直接 domHeight = dom[0].clientHeight;
if (dom&&dom.length) {
    // 过滤出 display: none 的元素
    let domArr = []
    for(let i=0; i<dom.length; i++) {
      if(dom[i].clientHeight !== 0) domArr.push(dom[i])
    }
    
    domHeight = domArr[domArr.length-1].clientHeight;
  }
  return domHeight;
}

 

标签:body,documentElement,视窗,clientHeight,dom,高度,获取,var,document
来源: https://www.cnblogs.com/Hhuizi/p/16599979.html