一些常用js方法汇总

1.判断当前网站是否被打开且是激活可见状态,是否被别的窗口遮住?

一些常用js方法汇总
/** 判断当前网站是否被打开,适配不同浏览器,返回不同的监听事件等 */
export function isHideDocument() {
  let hidden; let state; let visibilityChange;
  if (typeof document.hidden !== 'undefined') {
    hidden = 'hidden';
    visibilityChange = 'visibilitychange';
    state = 'visibilityState';
  } else if (typeof document.mozHidden !== 'undefined') {
    hidden = 'mozHidden';
    visibilityChange = 'mozvisibilitychange';
    state = 'mozVisibilityState';
  } else if (typeof document.msHidden !== 'undefined') {
    hidden = 'msHidden';
    visibilityChange = 'msvisibilitychange';
    state = 'msVisibilityState';
  } else if (typeof document.webkitHidden !== 'undefined') {
    hidden = 'webkitHidden';
    visibilityChange = 'webkitvisibilitychange';
    state = 'webkitVisibilityState';
  }
  return { hidden, visibilityChange, state }
}
View Code
// 如何使用该方法展示
 // 获取和监听页面状态
      const isHideDocumentInit = isHideDocument(); //获取当前运行浏览器适配的名字;
      let isHide = document[isHideDocumentInit.state];//是否当前是非激活状态
      this.$store.commit('SET_DOCUMENT_IS_HIDE', isHide === 'hidden')
      document.addEventListener(isHideDocumentInit.visibilityChange, () => { //添加是否可视的监听
        isHide = document[isHideDocumentInit.state]
        this.$store.commit('SET_DOCUMENT_IS_HIDE', isHide === 'hidden')
      }, false);

 

上一篇:1-预编译习题的讲解


下一篇:uni-app在小程序开发者工具:TypeError: Cannot read property ‘forceUpdate‘ of undefined