In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
本篇内容主要讲解"HTML5中有哪些简单实用的API",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"HTML5中有哪些简单实用的API"吧!
一、全屏API接口
强大的全屏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接口
页面可见性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
getUserMedia API是个非常有趣的接口!使用这个API,加上和标记,你可以在浏览器里进行拍照!
代码如下:
// 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
电池接口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 prefetch)API功能能够让浏览器在后台静悄悄的预先加载/读取一些页面或资源到当前页面,给用户一个顺滑的使用体验:
代码如下:
就是这5个你需要知道和尝试的新HTML5 API。请注意,这些新功能在几年之内就会流行起来,所以,越早接受这些API,你就能更好的创造出最前沿技术的Web应用。
到此,相信大家对"HTML5中有哪些简单实用的API"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.