首页 > 其他 > 详细

页面可见性事件

时间:2016-10-21 20:01:02      阅读:236      评论:0      收藏:0      [点我收藏+]

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Page Visibility API(兼容各种浏览器)</title>
</head>

<body onload="load()">
     <audio id="audio_id">
        <source src="always.mp3"/>
        <source src="demo-audio.ogg"/>
           Browser can‘t support Audio tag.
    </audio>
    <p>参考网址:https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API</p>

<script>
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
  hidden = "hidden";
  visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
  hidden = "msHidden";
  visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
  hidden = "webkitHidden";
  visibilityChange = "webkitvisibilitychange";
}
 
var videoElement = document.getElementById("audio_id");

// If the page is hidden, pause the video;
// if the page is shown, play the video
function handleVisibilityChange() {
  if (document[hidden]) {
    videoElement.pause();
  } else {
    videoElement.play();
  }
}

// Warn if the browser doesn‘t support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || typeof document[hidden] === "undefined") {
  console.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
  // Handle page visibility change   
  document.addEventListener(visibilityChange, handleVisibilityChange, false);
    
  // When the video pauses, set the title.
  // This shows the paused
  videoElement.addEventListener("pause", function(){
    document.title = ‘Paused‘;
  }, false);
    
  // When the video plays, set the title.
  videoElement.addEventListener("play", function(){
    document.title = ‘Playing‘;
  }, false);

}
</script>
</body>
</html>

页面可见性事件

原文:http://www.cnblogs.com/1124592765qq/p/5985587.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!