这次接着介绍5个更多HTML 5的API。这些API还是很实用的。

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"了


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);



不错的关于这个功能的详细教程:


​http://davidwalsh.name/page-visibility ​

3 getUserMedia api 获得访问媒体设备的API


可以允许调用用户的媒体设备,比如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);


更具体的教程见:


http://davidwalsh.name/browser-camera




4 电池API


可以获得电池的工作状态



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);


这个对移动设备很有效果,相关教程:


http://davidwalsh.name/battery-api


可惜这东西暂时只自mozilla下有效果



5 页面元素预加载 prefetch


主要是浏览器加载完页面后,将其放到浏览器的cache中去,


<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" />


教程:http://davidwalsh.name/html5-prefetch