pagehide:当页面点击链接跳走时触发
pageshow:当页面点击回退时触发
visibilitychange:会在页面被赢藏或显示的时候触发,比如手机应用切换,浏览器tab切换
示例:
window.addEventListener('pageshow', (e) => {
if (e.persisted) {
console.log('pageshow:页面回来了', new Date())
}
})
window.addEventListener('pagehide', (e) => {
if (e.persisted) {
console.log('document pageshow:页面跳走了', new Date())
}
})
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === "hidden" || document.hidden) {
console.log('document visibilitychange:页面隐藏了', new Date())
}
if (document.visibilityState === "visible" || !document.hidden) {
console.log('document visibilitychange:页面显示了', new Date())
}
})