日期:2014-05-16  浏览次数:20663 次

5个不可错过的HTML 5中的API 【转】

源自:http://jackyrong.iteye.com/blog/1722097

?

1 fullscreen全屏api,可惜不是全部浏览器支持,播放视频,做游戏时有用

function launchFullScreen(element) {
  if(element.requestFullScreen) {
    element.requestFullScreen();
  } else if(element.mozRequestFullScreen) {
    element.mozRequestFullScreen();
  } else if(element.webkitRequestFullScreen) {
    element.webkitRequestFullScreen();
  }
}

// Launch fullscreen for browsers that support it!
launchFullScreen(document.documentElement);

launchFullScreen(document.getElementById("videoElement")); //点单独的与元素时启动 

?

2 page visiablity页面可见性判断的API
?? 这个可以用来判断当用户的焦点不在当前tab的时候触发,下面这个例子,当CHROME下运行时候,如果新建立一个TAB,并且焦点到了新的tab,则会发现原来的页面的标题的title属性显示为"hidden"了 不错的关于这个功能的详细教程: http://davidwalsh.name/page-visibility

var hidden, state, 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";
}

// Add a listener that constantly changes the title
document.addEventListener(visibilityChange, function(e) {
  // Start or stop processing depending on state

}, false);

?3 getUserMedia api 获得访问媒体设备的API
?? 可以允许调用用户的媒体设备,比如camera等。? 更具体的教程见:
?? http://davidwalsh.name/browser-camera

window.addEventListener("DOMContentLoaded", function() {
  // Grab elements, create settings, etc.
  var canvas = document.getElementById("canvas"),
    context = canvas.getContext("2d"),
    video = document.getElementById("video"),
    videoObj = { "video": true },
    errBack = function(error) {
      console.log("Video capture error: ", error.code); 
    };

  // 播放
  if(navigator.getUserMedia) { // Standard
    navigator.getUserMedia(videoObj, function(stream) {
      video.src = stream;
      video.play();
    }, errBack);
  } else if(navigator.webkitGetUserMedia) { // WebKit系列浏览器    navigator.webkitGetUserMedia(videoObj, function(stream){
      video.src = window.webkitURL.createObjectURL(stream);
      video.play();
    }, errBack);
  }
}, false);

 

?4 电池API
?? 可以获得电池的工作状态?这个对移动设备很有效果,相关教程:
?? http://davidwalsh.name/battery-api 可惜这东西暂时只自mozilla下有效果

var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;


console.warn("Battery charging: ", battery.charging); // true
console.warn("Battery level: ", battery.level); // 0.58
console.warn("Battery discharging time: ", battery.dischargingTime);

//电池的监听 
battery.addEventListener("chargingchange", function(e) {
  console.warn("Battery charge change: ", battery.charging);
}, false);

?5 页面元素预加载 prefetch
? 主要是浏览器加载完页面后,将其放到浏览器的cache中去教程:http://davidwalsh.name/html5-prefetch

<link rel="prefetch" href="http://davidwalsh.name/css-enhancements-user-experience" />


<link rel="prefetch" href="http://davidwalsh.name/wp-content/themes/walshbook3/images/sprite.png" />

?