码迷,mamicode.com
首页 > Web开发 > 详细

用html5 js实现浏览器全屏

时间:2014-05-30 12:41:17      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:c   class   code   java   tar   http   

项目中需要将后台浏览器的窗口全屏,也就是我们点击一个按钮要实现按F11全屏的效果。 在HTML5中,W3C制定了关于全屏的API,就可以实现全屏幕的效果,也可以让页面中的图片,视频等全屏目前只有google chrome 15 +, safri5.1+,firfox10+,IE11支持

全屏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var docElm = document.documentElement;
//W3
if (docElm.requestFullscreen) { 
    docElm.requestFullscreen(); 
}
//FireFox 
else if (docElm.mozRequestFullScreen) { 
    docElm.mozRequestFullScreen(); 
}
//Chrome等 
else if (docElm.webkitRequestFullScreen) { 
    docElm.webkitRequestFullScreen(); 
}
//IE11
else if (elem.msRequestFullscreen) {
  elem.msRequestFullscreen();
}

退出全屏

1
2
3
4
5
6
7
8
9
10
11
12
if (document.exitFullscreen) { 
    document.exitFullscreen(); 
else if (document.mozCancelFullScreen) { 
    document.mozCancelFullScreen(); 
else if (document.webkitCancelFullScreen) { 
    document.webkitCancelFullScreen(); 
}
else if (document.msExitFullscreen) {
      document.msExitFullscreen();
}

事件监听

1
2
3
4
5
6
7
8
9
10
document.addEventListener("fullscreenchange", function () { 
    fullscreenState.innerHTML = (document.fullscreen)? "" : "not ";}, false); 
   
document.addEventListener("mozfullscreenchange", function () { 
    fullscreenState.innerHTML = (document.mozFullScreen)? "" : "not ";}, false); 
   
document.addEventListener("webkitfullscreenchange", function () { 
    fullscreenState.innerHTML = (document.webkitIsFullScreen)? "" : "not ";}, false);
document.addEventListener("msfullscreenchange", function () {
    fullscreenState.innerHTML = (document.msFullscreenElement)? "" : "not ";}, false);

全屏样式设置

在浏览器全屏的使用我们还可以进行样式设置

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

附录

1 一个在线的Demo

    http://robnyman.github.io/fullscreen/

2   HTML5全屏API之网络钓鱼

      http://www.36ria.com/5807

3   jquery封装的全屏插件

     http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/

4  更加详细的全屏API介绍

    4.1 https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode

    4.2  https://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html

用html5 js实现浏览器全屏,布布扣,bubuko.com

用html5 js实现浏览器全屏

标签:c   class   code   java   tar   http   

原文地址:http://www.cnblogs.com/javaee6/p/3756249.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!