5个不可错过的HTML 5中的API
这次接着介绍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")); //点单独的与元素时启动
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 titledocument.addEventListener(visibilityChange, function(e) { // Start or stop processing depending on state}, false);
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);
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;console.warn("Battery charging: ", battery.charging); // trueconsole.warn("Battery level: ", battery.level); // 0.58console.warn("Battery discharging time: ", battery.dischargingTime);//电池的监听 battery.addEventListener("chargingchange", function(e) { console.warn("Battery charge change: ", battery.charging);}, false);
<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" />