document visibilityState visibilitychange
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div class="con"></div> </body> <script type="text/javascript"> document.addEventListener("visibilitychange",function(){ if(document.visibilityState=="visible"){ console.log("欢迎回来!") //do something //继续视频播放 } if(document.visibilityState=="hidden"){ console.log("不要走!") //do something else //暂停视频播放 } }) </script> </html>