移动端获取元素计算后的尺寸

rdCss() {
  var obj = arguments[0];
  var options = arguments[1];
  if (!!obj === false || typeof options === 'undefined') {
    return null;
  }
  if (typeof options === 'string') {
    let res = document.defaultView.getComputedStyle(obj, null)[options];
    res = Number(res.replace('px', ''));
    return res;
  }
  if (typeof options === 'object') {
    for (var i in options) {
      obj.style[i] = options[i];
    }
  }
}

使用:

var dom=document.querySelectorAll('.better')[0]
var height=Number(that.rdCss(dom, 'height')); //获取了元素的高度

posted @ 2018-04-17 15:11  nancy_明月无缺  阅读(775)  评论(0编辑  收藏  举报