Home >Web Front-end >H5 Tutorial >5 powerful HTML5 API function recommendations_html5 tutorial skills

5 powerful HTML5 API function recommendations_html5 tutorial skills

WBOY
WBOYOriginal
2016-05-16 15:47:151405browse

HTML5 provides some very powerful JavaScript and HTML APIs to help developers build amazing desktop and mobile applications. This article will introduce 5 new APIs, hoping to be helpful to your development work.

1. Fullscreen API

This API allows developers to programmatically run web applications in full screen, making web applications more like native applications.


Copy the code
The code is as follows:

// Find the full screen method suitable for the browser
function launchFullScreen(element) {
if(element.requestFullScreen) {
element.requestFullScreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
}
}
// Launch full screen mode
launchFullScreen(document.documentElement); // the whole page
launchFullScreen(document.getElementById("videoElement")); // any individual element

 2. Page Visibility API (Page Visibility API)

This API can be used to detect the visibility of the page to the user, that is, return the status change of the page or tab currently browsed by the user.


Copy code
The code is as follows:

// Set hidden attributes and visible change events The name and attributes need to be added with the browser prefix
// 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 for title change
document.addEventListener(visibilityChange, function(e) {
// Start or stop status processing
}, false);


 3. getUserMedia API

This API allows web applications to access the camera and microphone without using plug-ins.


Copy code
The code is as follows:

// Set event listener
window.addEventListener("DOMContentLoaded", function() {
// Get element
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);
};
// Set video listener
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);

 4. Battery API

This is an API for mobile device applications, mainly used to detect device battery information.


Copy code
The code is as follows:

var 배터리 = navigator.battery || navigator.mozBattery;
// 배터리 속성
console.warn("배터리 충전: ", 배터리 충전) // true
console.warn("배터리 수준: ", Battery.level); // 0.58
console.warn("배터리 방전 시간: ", Battery.dischargeTime)// 이벤트 리스너 추가
🎜>battery.addEventListener("chargechange", function(e) {
console.warn("배터리 충전 변경: ", 배터리.충전);
}, false);

5. 링크 미리 가져오기

웹페이지 콘텐츠를 미리 로드하여 시청자에게 원활한 탐색 경험을 제공하세요.


코드 복사코드는 다음과 같습니다.


위의 5가지 새로운 API에 대해 이해하셨나요? 궁금한 점이 있으면 메시지를 남겨주세요.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn