强大的全屏API接口能让程序员通过编程启动浏览器进入全屏模式,并请求用户的允许:
// Find the right method, call on correct element 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); // the whole page launchFullScreen(document.getElementById("videoElement")); // any individual element
任何页面元素都可以成为全屏输出的目标,HTML5里甚至还提供了一个CSS伪类来让程序员在浏览器全屏时控制全屏元素的样式。当你在开发游戏时这个全屏API接口特别有用;尤其像BananaBread这样的枪击游戏中。
页面可见性API接口提供给用了一个监听事件,这个事件能告诉程序员当前页面是否是用浏览器中激活的标签页/窗口、是否是用户正在观看的页面,它还能告诉程序员用户何时切换页面、不再观看本页面/窗口:
// Adapted slightly from Sam Dutton // Set name of hidden property and visibility change event // since some browsers only offer vendor-prefixed support 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);
通过灵活的使用这个API,程序员可以在用户不观看本页时暂停一些繁重的任务(例如AJAX或动画)。
getUserMedia API是个非常有趣的接口!使用这个API,加上<video>和<canvas>标记,你可以在浏览器里进行拍照!
// Put event listeners into place 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); }; // Put video listeners into place if(navigator.getUserMedia) { // Standard navigator.getUserMedia(videoObj, function(stream) { video.src = stream; video.play(); }, errBack); } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed navigator.webkitGetUserMedia(videoObj, function(stream){ video.src = window.webkitURL.createObjectURL(stream); video.play(); }, errBack); } }, false);
你一定要在以后的应用中试试这个HTML5新功能,通过浏览器进行各种各样的交互的技术已经越来越流行了!
电池接口API很显然是专门为手机里的浏览器应用设计的,它提供了读取设备里的电池电量和充电状态的功能:
// Get the battery! var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery; // A few useful battery properties console.warn("Battery charging: ", battery.charging); // true console.warn("Battery level: ", battery.level); // 0.58 console.warn("Battery discharging time: ", battery.dischargingTime); // Add a few event listeners battery.addEventListener("chargingchange", function(e) { console.warn("Battery charge change: ", battery.charging); }, false);
这些HTML5提供的电池接口API能直接将电池电量状态告诉web应用,而不需要借助电池传感器或第三方应用。虽然不是一个特别大的功能,但绝对是一个有用的接口。
页面预加载(Link prefetch)API功能能够让浏览器在后台静悄悄的预先加载/读取一些页面或资源到当前页面,给用户一个顺滑的使用体验:
<!-- 预加载一个页面 --> <link rel="prefetch" href="http://www.webhek.com/link-prefetch/" /> <!-- 预加载一个图片 --> <link rel="prefetch" href="http://www.webhek.com/wordpress/wp-content/themes/webhek2/images/follow-us.jpg?bae1f7" />
就是这5个你需要知道和尝试的新HTML5 API。请注意,这些新功能在几年之内就会流行起来,所以,越早接受这些API,你就能更好的创造出最前沿技术的Web应用。花几分钟试试这些新功能,看看你能用它们实现什么样的效果!