摘要:毫無疑問,HTML5已經成爲當今最流行的一門技術,尤爲是Web開發者們對HTML5的興趣是日趨漸濃。HTML5的許多功能也都能在現代瀏覽器中得以實現。然而,做爲開發者,除了關注HTML5的功能和開發工具外,你有對其API留意過嗎?本文將爲你提供5個很是實用的API,絕對讓你收穫滿滿!php
HTML5革命給Web開發者們帶來許多超棒的JavaScript和HTML APIs,有些API已逐漸成爲他們的好幫手。最近,我又接觸到了5個很是實用的HTML5 API,在此和你們分享,但願這些API能幫助你開發出更好的Web Apps。css
1.Fullscreen APIhtml
全屏API,顧名思義,全屏API能夠讓開發人員進入到瀏覽器的全屏模式,用戶在使用的時候能夠隨意啓動和取消該模式。這個API特別適合遊戲開發者:html5
- // 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
2.Page Visibility APIcanvas
該API給開發者提供了一個監聽事件,能夠告訴開發者當前用戶關注的頁面TAB以及目前在窗口上所打開的頁面狀態。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";
3.getUserMedia APIide
這是個很是有趣的API,使用該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);
顯然,這是一款用在移動設備上的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);
點擊閱讀教程
Link Prefetching提供了連接預取功能,能夠讓開發者更清晰流暢的預加載網站內容,提升用戶體驗。
- <!-- full page -->
- <link rel="prefetch" href="http://davidwalsh.name/css-enhancements-user-experience" />
- <!-- just an image -->
- <link rel="prefetch" href="http://davidwalsh.name/wp-content/themes/walshbook3/images/sprite.png" />
點擊閱讀教程
這是5個值得Web開發者們細心研究的API,在將來會被普遍運用。利用這些API能夠幫你構建一些世界級的企業應用,不妨把它們組合起來使用看看!
更多精彩內容,請關注新浪微博@CSDN研發頻道
相關閱讀:你不知道的5個HTML5 API