5个功能强大的 HTML5 API

HTML5 新增了许多重要的特性,像 video、audio 和 canvas 等等,这些特性使得能够很容易的网页中包含多媒体内容,而不需要任何的插件或者 API。而其它的新元素,例如 section、article、header 和 nav 等则是用来丰富网页内容。另外还有很多强大的 JavaScript API,下面这5个 HTML5 API 你可能不知道,通过今天这篇文章可以了解一下。

Fullscreen API

  这个 HTML5 全屏 API 让 Web 开发者可以通过编程控制页面的全屏显示。这个 API 对于 JavaScript 游戏开发特别有用,例如这款单人射击游戏 BananaBread,在全屏状态下玩那是相当酷。

 



[backcolor=rgb(248, 248, 248) !important]1

2

[backcolor=rgb(248, 248, 248) !important]3

4

[backcolor=rgb(248, 248, 248) !important]5

6

[backcolor=rgb(248, 248, 248) !important]7

8

[backcolor=rgb(248, 248, 248) !important]9

10

[backcolor=rgb(248, 248, 248) !important]11

12

[backcolor=rgb(248, 248, 248) !important]13

14

 
[backcolor=rgb(248, 248, 248) !important]// 根据目标元素调用相应的方法
function launchFullScreen(element) {
[backcolor=rgb(248, 248, 248) !important] if(element.requestFullScreen) {
element.requestFullScreen();
[backcolor=rgb(248, 248, 248) !important] } else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
[backcolor=rgb(248, 248, 248) !important] } else if(element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
[backcolor=rgb(248, 248, 248) !important] }
}

// 在支持的浏览器中启动全屏
[backcolor=rgb(248, 248, 248) !important]launchFullScreen(document.documentElement); // 整个页面
launchFullScreen(document.getElementById("videoElement")); // 单个元素

 


  使用教程 在线演示

Page Visibility API
  Page Visibility API 可以帮助开发者监听用户的焦点在哪个页面选项卡上面以及用户在选项库或者窗口之间的移动情况。如果使用合理的话,当焦点不在某个页面上的时候可以停止一些消耗很大的任务。
[size=1em]
[color=rgb(255, 255, 255) !important]?
 

[backcolor=rgb(248, 248, 248) !important]1

2

[backcolor=rgb(248, 248, 248) !important]3

4

[backcolor=rgb(248, 248, 248) !important]5

6

[backcolor=rgb(248, 248, 248) !important]7

8

[backcolor=rgb(248, 248, 248) !important]9

10

[backcolor=rgb(248, 248, 248) !important]11

12

[backcolor=rgb(248, 248, 248) !important]13

14

[backcolor=rgb(248, 248, 248) !important]15

16

[backcolor=rgb(248, 248, 248) !important]17

18

[backcolor=rgb(248, 248, 248) !important]19

20

[backcolor=rgb(248, 248, 248) !important]21

22

[backcolor=rgb(248, 248, 248) !important]23

24

[backcolor=rgb(248, 248, 248) !important]25

 
[backcolor=rgb(248, 248, 248) !important]// 部分浏览器只支持 vendor-prefixed
// 根据浏览器支持情况设置隐藏属性和可见状态改变事件
[backcolor=rgb(248, 248, 248) !important]var hidden, state, visibilityChange; 
if (typeof document.hidden !== "undefined") {
[backcolor=rgb(248, 248, 248) !important] hidden = "hidden";
visibilityChange = "visibilitychange";
[backcolor=rgb(248, 248, 248) !important] state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
[backcolor=rgb(248, 248, 248) !important] hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
[backcolor=rgb(248, 248, 248) !important] state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
[backcolor=rgb(248, 248, 248) !important] hidden = "msHidden";
visibilityChange = "msvisibilitychange";
[backcolor=rgb(248, 248, 248) !important] state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
[backcolor=rgb(248, 248, 248) !important] hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
[backcolor=rgb(248, 248, 248) !important] state = "webkitVisibilityState";
}

// 添加一个时间来实时改变页面的标题
[backcolor=rgb(248, 248, 248) !important]document.addEventListener(visibilityChange, function(e) {
// Start or stop processing depending on state
[backcolor=rgb(248, 248, 248) !important]}, false);

 


  使用教程 在线演示

Supporting visibilityChange in MooTools
MooTools doesn't support visibilityChange out of the box, so you'll need to add this bit of JavaScript:
// Set it up!Element.NativeEvents[visibilityChange] = 2;Element.Events[visibilityChange] = { base: visibilityChange, condition: function(event) { event[state] = document[state]; event.visibilityState = document[state]; return true; }};// Now use it!document.addEvent(visibilityChange, function(e) { document.title = document[state];});
Don't you love it when it's that easy?! This mirror the code needed to add onMessage events to the list of supported events.

getUserMedia API
  特别有趣的一个 API,能够调用电脑的摄像头,结合 <video> 标签和 Canvas 就能在浏览器中拍摄照片了。未来这个 API 将被广泛用来让浏览器和用户之间互动。
[size=1em]
[color=rgb(255, 255, 255) !important]?
 

[backcolor=rgb(248, 248, 248) !important]1

2

[backcolor=rgb(248, 248, 248) !important]3

4

[backcolor=rgb(248, 248, 248) !important]5

6

[backcolor=rgb(248, 248, 248) !important]7

8

[backcolor=rgb(248, 248, 248) !important]9

10

[backcolor=rgb(248, 248, 248) !important]11

12

[backcolor=rgb(248, 248, 248) !important]13

14

[backcolor=rgb(248, 248, 248) !important]15

16

[backcolor=rgb(248, 248, 248) !important]17

18

[backcolor=rgb(248, 248, 248) !important]19

20

[backcolor=rgb(248, 248, 248) !important]21

22

[backcolor=rgb(248, 248, 248) !important]23

24

 
[backcolor=rgb(248, 248, 248) !important]// 添加事件监听器
window.addEventListener("DOMContentLoaded", function() {
[backcolor=rgb(248, 248, 248) !important] // 获取元素,创建配置
var canvas = document.getElementById("canvas"),
[backcolor=rgb(248, 248, 248) !important] context = canvas.getContext("2d"),
video = document.getElementById("video"),
[backcolor=rgb(248, 248, 248) !important] videoObj = { "video": true },
errBack = function(error) {
[backcolor=rgb(248, 248, 248) !important] console.log("Video capture error: ", error.code); 
};

// 添加视频监听器
[backcolor=rgb(248, 248, 248) !important] if(navigator.getUserMedia) { // 标准的API
navigator.getUserMedia(videoObj, function(stream) {
[backcolor=rgb(248, 248, 248) !important] video.src = stream;
video.play();
[backcolor=rgb(248, 248, 248) !important] }, errBack);
} else if(navigator.webkitGetUserMedia) { // WebKit 核心的API
[backcolor=rgb(248, 248, 248) !important] navigator.webkitGetUserMedia(videoObj, function(stream){
video.src = window.webkitURL.createObjectURL(stream);
[backcolor=rgb(248, 248, 248) !important] video.play();
}, errBack);
[backcolor=rgb(248, 248, 248) !important] }
}, false);

 


  使用教程 在线演示

Battery API
  顾名思义,这是一个电池 API ,明显是为移动设备准备的,用于监控电池的状态。能够通过事件监听电池电量的变化,电池的等级、可用时间等状态。下面是是示例代码,可以通过后面的教程链接学习更详细的使用方法。
[size=1em]
[color=rgb(255, 255, 255) !important]?
 

[backcolor=rgb(248, 248, 248) !important]1

2

[backcolor=rgb(248, 248, 248) !important]3

4

[backcolor=rgb(248, 248, 248) !important]5

6

[backcolor=rgb(248, 248, 248) !important]7

8

[backcolor=rgb(248, 248, 248) !important]9

10

[backcolor=rgb(248, 248, 248) !important]11

12

 
[backcolor=rgb(248, 248, 248) !important]// 获取电池对象
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;

// 一组非常有用的电池属性
[backcolor=rgb(248, 248, 248) !important]console.warn("Battery charging: ", battery.charging); // true
console.warn("Battery level: ", battery.level); // 0.58
[backcolor=rgb(248, 248, 248) !important]console.warn("Battery discharging time: ", battery.dischargingTime);

[backcolor=rgb(248, 248, 248) !important]// 监听电池状态
battery.addEventListener("chargingchange", function(e) {
[backcolor=rgb(248, 248, 248) !important] console.warn("Battery charge change: ", battery.charging);
}, false);

 


  使用教程

Link Prefetching
  这个链接预取 API 非常有用,让开发者可以控制网页资源在后台安静的预先加载,这样用户在浏览网站或者使用 Web 应用程序的时候能够有流畅的使用体验。可以预加载整个页面,也可以是单个资源,示例代码如下:
[size=1em]
[color=rgb(255, 255, 255) !important]?
 

[backcolor=rgb(248, 248, 248) !important]1

2

[backcolor=rgb(248, 248, 248) !important]3

4

[backcolor=rgb(248, 248, 248) !important]5

 
[backcolor=rgb(248, 248, 248) !important]<!-- 预加载整个页面 -->
<link rel="prefetch" href="[color=blue !important]http://davidwalsh.name/css-enhancements-user-experience" />
[backcolor=rgb(248, 248, 248) !important] 
< !-- 预加载一张图片 -->
[backcolor=rgb(248, 248, 248) !important]<link rel="prefetch" href="[color=blue !important]http://img.laike.net/html520200222040148.png" />

 


  使用教程

  上面这5个 HTML5 API 值得关注,在不久的将来这些 API 将被广泛的使用,因此越早掌握它们可以帮助你为构建优秀的 Web 应用打下坚实的基础。通过使用教程可以快速的熟悉这些 API 的基本用法和使用场景,提供的在线演示展示了直观的应用情况。