如果想要監(jiān)聽(tīng)到網(wǎng)頁(yè)是否處于后臺(tái)或者切換了瀏覽器標(biāo)題頁(yè)惋鹅,可以在創(chuàng)建一個(gè)監(jiān)聽(tīng)事件
例子如下
// 監(jiān)視頁(yè)面標(biāo)簽頁(yè)是否處于激活狀態(tài)
window.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
console.log('前臺(tái)')
} else if (document.visibilityState === 'hidden') {
console.log('后臺(tái)')
}
})