HTML5中与页面彰显相关的API

HTML5中与页面突显相关的API

2015/05/15 · HTML5 ·
HTML5

最初的稿件出处:
涂根华的博客   

在HTML5中,增添了2个与页面呈现相关的API,分别是Page Visibility
API与Fullscreen API; 作用分别如下:

Page Visibility API 
是指当页面变为最小化状态恐怕客商将浏览器标签切换成其余标签时会触发。

Fullscreen API 是将页面全体或页面中有些局地区域设为全屏。

Page Visibility API的选拔场合如下:

  1. 三个应用程序中有所多幅图片的幻灯片式的再三再四播发效果,当页面变为不可以看到状态(最小化状态可能将用户浏览器标签切换成任何标签时),图片停播,当页面变为可知状态时,图片继续播放。
  2. 在三个实时呈现服务器端音讯的应用程序中,当页面处于不可以预知状态(最小化状态大概将顾客浏览器标签切换成此外标签时),截止依期向服务器端央求数据的拍卖,当页面变为可以见到状态,继续履行定时向劳动器端央求数据的拍卖。
  3. 在贰个兼有播放录像功效的应用程序中,当页面处于不可以预知状态(最小化状态可能将客商浏览器标签切换来任何标签时),暂停止播放放录制,当页面变为可以知道状态时,继续播放录制。

浏览器援救程度:Firefox10+,chrome14+,IE10+;

实现Page Visibility API

在利用Page Visibility
API时,我们先是供给看清当前顾客所接纳的浏览器以至该浏览器是或不是协助。代码如下决断:

JavaScript

if(typeof document.hidden !== ‘undefined’) { hidden = ‘hidden’;
visibilityChange = ‘visibilitychange’; }else if(typeof
document.mozHidden !== ‘undefined’) { hidden = ‘mozHidden’;
visibilityChange = ‘mozvisibilitychange’; }else if(typeof
document.msHidden !== ‘undefined’) { hidden = ‘msHidden’;
visibilityChange = ‘msvisibilitychange’; }else if(typeof
document.webkitHidden !== ‘undefined’) { hidden = ‘webkitHidden’;
visibilityChange = ‘webkitvisibilitychange’; }

1
2
3
4
5
6
7
8
9
10
11
12
13
if(typeof document.hidden !== ‘undefined’) {
    hidden = ‘hidden’;
    visibilityChange = ‘visibilitychange’;
}else if(typeof document.mozHidden !== ‘undefined’) {
    hidden = ‘mozHidden’;
    visibilityChange = ‘mozvisibilitychange’;
}else if(typeof document.msHidden !== ‘undefined’) {
    hidden = ‘msHidden’;
    visibilityChange = ‘msvisibilitychange’;
}else if(typeof document.webkitHidden !== ‘undefined’) {        
    hidden = ‘webkitHidden’;
    visibilityChange = ‘webkitvisibilitychange’;
}

如上,在Page Visibility
 API中,能够经过document对象的hidden属性值来推断页面是或不是处于可以知道状态,当页面处于可以预知状态时属性值为false,当页面处于不可知状态时属性值为true。

在Page
Visibility中,能够经过document对象的visibilityState属性值来判别页面包车型地铁可以知道状态。该属性值为三个字符串,其含义如下所示:

   
visible:
 页面内容部分可以预知,当前页面位于顾客正在查看的浏览器标签窗口中,且浏览器窗口未被最小化。

   
hidden:
 页面内容对顾客不可以知道。当前页面不在客商正在查阅的浏览器标签窗口中,或浏览器窗口已被最小化。

    prerender: 页面内容已被预渲染,但是对客户不可以知道。

这几天我们来看一个demo,页面中有三个video成分与贰个”播放”按键,顾客单击”播放”按键时
按键文字变为
’暂停”,同有时候开头广播video成分的录像,当页面变为最小化状态或客户浏览器标签切换到任何标签时候,摄像被暂停止播放放,当页面复苏不荒谬状态或顾客将浏览器标签切回页面所在标签时,录像持续播放。

HTML代码如下:

JavaScript

<video id=”videoElement” controls width=640 height=360 autoplay>
<source src=”Wildlife/Wildlife.ogv” type=’video/ogg; codecs=”theora,
vorbis”‘/> <source src=”Wildlife/Wildlife.webm” type=’video/webm’
> <source src=”Wildlife/Wildlife.mp4″ type=’video/mp4′>
</video> <button id=”btnPlay”
onclick=”PlayOrPause()”>播放</button> <div
style=”height:1500px;”></div>

1
2
3
4
5
6
7
<video id="videoElement" controls width=640 height=360 autoplay>
    <source src="Wildlife/Wildlife.ogv" type=’video/ogg; codecs="theora, vorbis"’/>
    <source src="Wildlife/Wildlife.webm" type=’video/webm’ >
    <source src="Wildlife/Wildlife.mp4" type=’video/mp4′>
</video>
<button id="btnPlay" onclick="PlayOrPause()">播放</button>
<div style="height:1500px;"></div>

JS代码如下:

JavaScript

var hidden, visibilityChange, videoElement; if(typeof document.hidden
!== ‘undefined’) { hidden = ‘hidden’; visibilityChange =
‘visibilitychange’; }else if(typeof document.mozHidden !== ‘undefined’)
{ hidden = ‘mozHidden’; visibilityChange = ‘mozvisibilitychange’; }else
if(typeof document.msHidden !== ‘undefined’) { hidden = ‘msHidden’;
visibilityChange = ‘msvisibilitychange’; }else if(typeof
document.webkitHidden !== ‘undefined’) { hidden = ‘webkitHidden’;
visibilityChange = ‘webkitvisibilitychange’; }
document.addEventListener(visibilityChange,handle,false); videoElement =
document.getElementById(“videoElement”);
videoElement.addEventListener(‘ended’,videoEnded,false);
videoElement.add伊夫ntListener(‘play’,videoPlay,false);
videoElement.addEventListener(‘pause’,videoPause,false); //
假使页面变为不可以知道状态 则暂停摄像播放 //
如若页面变为可以预知状态,则再而三摄像播放 function handle() { //
通过visibilityState属性值判断页面包车型客车可以知道状态
console.log(document.visibilityState); if(document[hidden]) {
videoElement.pause(); }else { videoElement.play(); } } // 播放摄像function play() { videoElement.play(); } // 暂停止播放放 function pause() {
videoElement.pause(); } function PlayOrPause() { if(videoElement.paused)
{ videoElement.play(); }else { videoElement.pause(); } } function
videoEnded(e) { videoElement.currentTime = 0; this.pause(); } function
videoPlay(e) { var btnPlay = document.getElementById(“btnPlay”);
btnPlay.innerHTML = “暂停”; } function videoPause(e) { var btnPlay =
document.getElementById(“btnPlay”); btnPlay.innerHTML = “播放”; }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var hidden,
visibilityChange,
videoElement;
if(typeof document.hidden !== ‘undefined’) {
    hidden = ‘hidden’;
    visibilityChange = ‘visibilitychange’;
}else if(typeof document.mozHidden !== ‘undefined’) {
    hidden = ‘mozHidden’;
    visibilityChange = ‘mozvisibilitychange’;
}else if(typeof document.msHidden !== ‘undefined’) {
    hidden = ‘msHidden’;
    visibilityChange = ‘msvisibilitychange’;
}else if(typeof document.webkitHidden !== ‘undefined’) {
    hidden = ‘webkitHidden’;
    visibilityChange = ‘webkitvisibilitychange’;
}
document.addEventListener(visibilityChange,handle,false);
videoElement = document.getElementById("videoElement");
videoElement.addEventListener(‘ended’,videoEnded,false);
videoElement.addEventListener(‘play’,videoPlay,false);
videoElement.addEventListener(‘pause’,videoPause,false);
// 如果页面变为不可见状态 则暂停视频播放
// 如果页面变为可见状态,则继续视频播放
function handle() {
    // 通过visibilityState属性值判断页面的可见状态
    console.log(document.visibilityState);
    if(document[hidden]) {
        videoElement.pause();    
    }else {
        videoElement.play();
    }
}
// 播放视频
function play() {
    videoElement.play();
}
// 暂停播放
function pause() {
    videoElement.pause();
}
function PlayOrPause() {
    if(videoElement.paused) {
        videoElement.play();
    }else {
        videoElement.pause();
    }
}
function videoEnded(e) {
    videoElement.currentTime = 0;
    this.pause();
}
function videoPlay(e) {
    var btnPlay = document.getElementById("btnPlay");
    btnPlay.innerHTML = "暂停";
}
function videoPause(e) {
    var btnPlay = document.getElementById("btnPlay");
    btnPlay.innerHTML = "播放";
}

实现Fullscreen API

在HTML5中,新扩大了多个Fullscreen
API,其成效是将页面全体或页面中有些局地区域设为全屏显示状态。

浏览器援救程度:Firefox10+,chrome16+,Safari5.1+

在Fullscreen
API中,能够经过DOM对象的根节点目的或某些成分的requestFullscreen属性值和推行绝对应的秘籍来判定浏览器是还是不是支持Fullscreen
API。代码如下:

JavaScript

var docElm = document.documentElement; if(docElm.requestFullscreen) {
docElm.requestFullscreen(); }else if(docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen(); }else if(docElm.webkitRequestFullScreen)
{ docElm.webkitRequestFullScreen(); }

1
2
3
4
5
6
7
8
var docElm = document.documentElement;
if(docElm.requestFullscreen) {
        docElm.requestFullscreen();
}else if(docElm.mozRequestFullScreen) {
        docElm.mozRequestFullScreen();
}else if(docElm.webkitRequestFullScreen) {
        docElm.webkitRequestFullScreen();
}

在Fullscreen
API中,也足以经过DOM对象或有个别成分的exitFullscreen与CanvelFullScreen属性和办法将眼下页面或有些成分设定为非全屏显示状态。

正如代码:

JavaScript

if(document.exitFullscreen) { document.exitFullscreen(); }else
if(document.mozCancelFullScreen) { document.mozCancelFullScreen(); }else
if(document.webkitCancelFullScreen) { document.webkitCancelFullScreen();
}

1
2
3
4
5
6
7
if(document.exitFullscreen) {
    document.exitFullscreen();
}else if(document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
}else if(document.webkitCancelFullScreen) {
    document.webkitCancelFullScreen();
}

在Fullscreen
API中,能够通过监听DOM对象或有个别成分的fullscreenchange事件(当页面或因素从非全屏呈现状态变为全屏显示状态,或从全屏呈现状态成为非全屏呈现状态时触发)。代码如下:

JavaScript

document.addEventListener(‘fullscreenchange’,function(){},false);
document.addEventListener(‘mozfullscreenchange’,function(){},false);
document.addEventListener(‘webkitfullscreenchange’,function(){},false);

1
2
3
document.addEventListener(‘fullscreenchange’,function(){},false);
document.addEventListener(‘mozfullscreenchange’,function(){},false);
document.addEventListener(‘webkitfullscreenchange’,function(){},false);

在css样式代码中,我们能够动用伪类选择器来单独钦点处于全屏展现状态的页面或因素样式:

JavaScript

html:-moz-full-screen { background:red; } html:-webkit-full-screen {
background:red; } html:fullscreen { background:red; }

1
2
3
4
5
6
7
8
9
html:-moz-full-screen {
    background:red;
}
html:-webkit-full-screen {
    background:red;
}
html:fullscreen {
    background:red;
}

末尾大家来看八个demo,在页面中有叁个按键,点击后,页面会产生全屏状态,再点击后,页面会退出全屏;

HTML代码如下:

JavaScript

<input type=”button” id=”btnFullScreen” value=”页面全屏展现”
onclick=”toggleFullScreen();”/> <div style=”width:百分百;”
id=”fullscreentState”>非全屏突显</div>

1
2
<input type="button" id="btnFullScreen" value="页面全屏显示" onclick="toggleFullScreen();"/>
<div style="width:100%;" id="fullscreentState">非全屏显示</div>

Javascript如下:

JavaScript

var docElm = document.documentElement; var fullscreentState =
document.getElementById(“fullscreentState”); var btnFullScreen =
document.getElementById(“btnFullScreen”); fullscreentState.style.height
= docElm.clientHeight + ‘px’;
document.add伊夫ntListener(‘fullscreenchange’,function(){
fullscreentState.innerHTML = (document.fullscreen) ? “全屏展现” :
“非全屏呈现”; },false);
document.addEventListener(‘mozfullscreenchange’,function(){
fullscreentState.innerHTML = (document.mozFullscreen) ? “全屏彰显” :
“非全屏展现”; },false);
document.addEventListener(‘webkitfullscreenchange’,function(){
fullscreentState.innerHTML = (document.webkitFullscreen) ? “全屏突显” :
“非全屏展现”; },false); function toggleFullScreen() {
if(btnFullScreen.value == ‘页面全屏展现’) { btnFullScreen.value =
‘页面非全屏显示’; if(docElm.requestFullscreen) {
docElm.requestFullscreen(); }else if(docElm.mozRequestFullScreen) {
docElm.mozRequestFullScreen(); }else if(docElm.webkitRequestFullScreen)
{ docElm.webkitRequestFullScreen(); } }else {
if(document.exitFullscreen) { document.exitFullscreen(); }else
if(document.mozCancelFullScreen) { document.mozCancelFullScreen(); }else
if(document.webkitCancelFullScreen) { document.webkitCancelFullScreen();
} btnFullScreen.value = “页面全屏呈现”; } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var docElm = document.documentElement;
var fullscreentState = document.getElementById("fullscreentState");
var btnFullScreen = document.getElementById("btnFullScreen");
fullscreentState.style.height = docElm.clientHeight + ‘px’;
document.addEventListener(‘fullscreenchange’,function(){
    fullscreentState.innerHTML = (document.fullscreen) ? "全屏显示" : "非全屏显示";
},false);
document.addEventListener(‘mozfullscreenchange’,function(){
    fullscreentState.innerHTML = (document.mozFullscreen) ? "全屏显示" : "非全屏显示";
},false);
document.addEventListener(‘webkitfullscreenchange’,function(){
    fullscreentState.innerHTML = (document.webkitFullscreen) ? "全屏显示" : "非全屏显示";
},false);
function toggleFullScreen() {
    if(btnFullScreen.value == ‘页面全屏显示’) {
        btnFullScreen.value = ‘页面非全屏显示’;
        if(docElm.requestFullscreen) {
            docElm.requestFullscreen();
        }else if(docElm.mozRequestFullScreen) {
            docElm.mozRequestFullScreen();
        }else if(docElm.webkitRequestFullScreen) {
            docElm.webkitRequestFullScreen();
        }
    }else {
        if(document.exitFullscreen) {
            document.exitFullscreen();
        }else if(document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        }else if(document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        }
        btnFullScreen.value = "页面全屏显示";
    }
}

赞 收藏
评论

图片 1

HTML5提供了部分非常强盛的JavaScript和HTML
API,来帮忙开拓者构建美好的桌面和活动应用程序。本文将介绍5个新型的API,希望对您的费用工作富有利于。

 

  1.  全屏API(Fullscreen API)

 

  该API允许开辟者以编程形式将Web应用程序全屏运转,使Web应用程序更像本地应用程序。

 

 

//

 找到切合浏览器的全屏方法

 

function

 

launchFullScreen(element) {

 

  if(element.requestFullScreen)

 {

 

    element.requestFullScreen();

 

  }

else

 

if(element.mozRequestFullScreen)

 {

 

    element.mozRequestFullScreen();

 

  }

else

 

if(element.webkitRequestFullScreen)

 {

 

    element.webkitRequestFullScreen();

 

  }

 

}

 

  

 

//

 运维全屏格局

 

launchFullScreen(document.documentElement);

//

 the whole page

 

launchFullScreen(document.getElementById(“videoElement”));

//

 any individual element

  教程 / 演示

 

  2.  页面可知性API(Page Visibility API)

 

  该API能够用来检查实验页面前遇到于顾客的可以预知性,即重临客商日前浏览的页面或标签的情形变化。

 

 

//

 设置隐瞒属性和可以看到改动事件的名称,属性需求加浏览器前缀

 

//

 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”;

 

}

 

  

 

//

 增多二个题名更改的监听器

 

document.addEventListener(visibilityChange,

function(e)

 {

 

  //

 初叶或终止状态管理

 

},

false);

  教程 / 演示

 

  3.  getUserMedia API

 

  该API允许Web应用程序访问摄像头和Mike风,而不须要利用插件。

相关文章