1.判断当前网站是否被打开且是激活可见状态,是否被别的窗口遮住?
/** 判断当前网站是否被打开,适配不同浏览器,返回不同的监听事件等 */ 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 } }
// 如何使用该方法展示 // 获取和监听页面状态 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);