HTML5提供了一些非常强大的JavaScript和HTML API,来帮助开发者构建精彩的桌面和移动应用程序。本文将介绍5个新型的API,希望对你的开发工作有所帮助。
该API允许开发者以编程方式将Web应用程序全屏运行,使Web应用程序更像本地应用程序。
//找到合适的浏览器全屏方法 element.requestFullScreen();
function launchFullScreen(element) {
if(element.requestFullScreen) {
element.requestFullScreen();
} else if(element.mozRequestFullScreen) {
lement.mozRequestFullScreen();
} else if(element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
}
}
//启动全屏模式 常用于视频
launchFullScreen(document.getElementById("videoElement"));
//退出全屏模式
document.cancelFullScreen();
document.mozCancelFullScreen();
document.webkitCancelFullScreen();
//添加全屏变化事件
element.addEventListener(‘fullscreenchange’, function(e) {
if (document.fullScreen) { // document.webkitIsFullScreen
/* make it look good for fullscreen */
} else {
/* return to the normal state in page */
}
}, false);
//看很多文章都是这样写 但是我试验了一下 好像木有反应
//我再手机播放视频通常使用
video.addEventListener(‘webkitendfullscreen‘,
function(e) {
alert("退出全屏")
},
false);
video.addEventListener(‘webkitbeginfullscreen‘, function(e) {
alert("进入")
},
false);
2. 页面可见性API(Page Visibility API)
该API可以用来检测页面对于用户的可见性,即返回用户当前浏览的页面或标签的状态变化。
//设置页面可见性变化监听
document.addEventListener("visibilitychange",stateChanged); document.addEventListener("webkitvisibilitychange", stateChanged); document.addEventListener("msvisibilitychange", stateChanged); function stateChanged(){ console.log(document.webkitVisibilityState); if(document.hidden || document.webkitHidden || document.msHidden){ alert("hidden") } }
预加载网页内容,为浏览者提供一个平滑的浏览体验。
<!-- full page -->
<link rel="prefetch" href="http://davidwalsh.name/css-enhancements-user-experience" />
<!-- just an image -->
4.DeviceOrientationEvent是获取方向,得到device静止时的绝对值;
DeviceMotionEvent是获取移动速度,得到device移动时相对之前某个时间的差值比
设备定位API,该API允许你收集设备的方向和移动信息。此外,该API只在具备陀螺仪功能的设备上使用。
<html>
<head>
<title>DeviceOrientationEvent</title>
<meta charset="UTF-8" />
</head>
<body>
<p>左右:<span id="alpha">0</span>
</p>
<p>前后:<span
id="beta">0</span>
</p>
<p>扭转:<span
id="gamma">0</span>
</p>
<p>指北针指向:<span
id="heading">0</span>度</p>
<p>指北针精度:<span
id="accuracy">0</span>度</p>
<hr />
<p>x轴加速度:<span id="x">0</span>米每二次方秒</p>
<p>y轴加速度:<span
id="y">0</span>米每二次方秒</p>
<p>z轴加速度:<span
id="z">0</span>米每二次方秒</p>
<hr />
<p>x轴加速度(考虑重力加速度):<span
id="xg">0</span>米每二次方秒</p>
<p>y轴加速度(考虑重力加速度):<span
id="yg">0</span>米每二次方秒</p>
<p>z轴加速度(考虑重力加速度):<span
id="zg">0</span>米每二次方秒</p>
<hr />
<p>左右旋转速度:<span
id="Ralpha">0</span>度每秒</p>
<p>前后旋转速度:<span
id="Rbeta">0</span>度每秒</p>
<p>扭转速度:<span
id="Rgamma">0</span>度每秒</p>
<hr />
<p>上次收到通知的间隔:<span
id="interval">0</span>毫秒</p>
<script
type="text/javascript">
function orientationHandler(event) {
document.getElementById("alpha").innerHTML =
event.alpha;
document.getElementById("beta").innerHTML = event.beta;
document.getElementById("gamma").innerHTML =
event.gamma;
document.getElementById("heading").innerHTML =
event.webkitCompassHeading;
document.getElementById("accuracy").innerHTML =
event.webkitCompassAccuracy;
}
function motionHandler(event)
{
document.getElementById("interval").innerHTML = event.interval;
var acc =
event.acceleration;
document.getElementById("x").innerHTML = acc.x;
document.getElementById("y").innerHTML = acc.y;
document.getElementById("z").innerHTML =
acc.z;
var accGravity =
event.accelerationIncludingGravity;
document.getElementById("xg").innerHTML = accGravity.x;
document.getElementById("yg").innerHTML =
accGravity.y;
document.getElementById("zg").innerHTML = accGravity.z;
var rotationRate = event.rotationRate;
document.getElementById("Ralpha").innerHTML =
rotationRate.alpha;
document.getElementById("Rbeta").innerHTML = rotationRate.beta;
document.getElementById("Rgamma").innerHTML
= rotationRate.gamma;
}
if (window.DeviceMotionEvent)
{
window.addEventListener("devicemotion", motionHandler, false);
} else {
document.body.innerHTML = "What user agent u r
using???";
}
if (window.DeviceOrientationEvent)
{
window.addEventListener("deviceorientation", orientationHandler,
false);
} else
{
document.body.innerHTML =
"What user agent u r using???";
};
</script>
</body>
</html>