兼容各种浏览器常用的原生js工具类:获取浏览器滚动条宽高、事件、局部打印等等

兼容各种浏览器常用的原生js工具类:获取浏览器滚动条宽高、事件、局部打印等等

一、获取浏览器可见域的宽高(不包括滚动条和被卷去)

ieE<=8 不支持innerWidth和innerHeight属性,ie6支持
document.documentElement.clientHeight,其他ie版本用document.body.clientHeight

export function getWinHeight() {
  var windowHeight;
  if (window.innerHeight) { // 除了IE 8 以外的浏览器
    windowHeight = window.innerHeight
  } else if (document.documentElement && document.documentElement.clientHeight) { /* IE6 浏览器 */
    windowHeight = document.documentElement.clientHeight
  } else if (document.body) { //其他版本的IE浏览器
    windowHeight = document.body.clientHeight
  }
  return windowHeight;
}
export function getWinWidth() {
  var windowWidth;
  if (window.innerWidth) { // 除了IE以外的浏览器
    windowWidth = window.innerWidth
  } else if (document.documentElement && document.documentElement.clientWidth) { /* IE6 浏览器 */
    windowWidth = document.documentElement.clientWidth
  } else if (document.body) { //其他版本的IE浏览器
    windowWidth = document.body.clientWidth
  }
  return windowWidth;
}

clientHeight/Width指如下:
在这里插入图片描述

二、事件的添加与移除

ieE<=8 不支持document.addEventListener事件添加与document.removeEventListener事件移除

export const on = (function() {
  if (document.addEventListener) {
    return function(element, event, handler) {
      if (element && event && handler) {
        element.
        (event, handler, false);
      }
    };
  } else {
    return function(element, event, handler) {
      if (element && event && handler) {
        element.attachEvent('on' + event, handler);
      }
    };
  }
})();
export const off = (function() {
  if (document.removeEventListener) {
    return function(element, event, handler) {
      if (element && event) {
        element.removeEventListener(event, handler, false);
      }
    };
  } else {
    return function(element, event, handler) {
      if (element && event) {
        element.detachEvent('on' + event, handler);
      }
    };
  }
})();

三、获取滚动条宽

ie不支持浏览器滚动条的设定与获取。elementUI2.13.2版本中获取滚动条方法如下:

export scrollbarWidth function() {
  const outer = document.createElement('div');
  outer.className = 'el-scrollbar__wrap';
  outer.style.visibility = 'hidden';
  outer.style.width = '100px';
  outer.style.position = 'absolute';
  outer.style.top = '-9999px';
  document.body.appendChild(outer);

  const widthNoScroll = outer.offsetWidth;
  outer.style.overflow = 'scroll';

  const inner = document.createElement('div');
  inner.style.width = '100%';
  outer.appendChild(inner);

  const widthWithScroll = inner.offsetWidth;
  outer.parentNode.removeChild(outer);
  scrollBarWidth = widthNoScroll - widthWithScroll;

  return scrollBarWidth;
};

☞戳源码

四、实现局部打印功能

/**
  * @param {DOM} dom 对象
  * @return {Null} null 实现局部打印功能
  */
export const printDom = (dom) => {
  let print = document.getElementById("print_dom_version1")
  if (!print) {
    let dom = document.createElement("div")
    dom.id = "print_dom_version1"
    document.body.appendChild(dom)
    print = dom
  }
  print.style.display = ''
  print.innerHTML = dom.innerHTML
  let bodyCh = document.body.children
  let dis = bodyCh[0].style.display
  bodyCh[0].style.display = 'none'
  window.print()
  print.style.display = 'none'
  bodyCh[0].style.display = dis
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值