欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript开发中如何利用浏览器全屏api实现js全屏

程序员文章站 2022-04-19 15:17:19
代码如下: (function () {  var fullscreenapi = {   supportsfullscreen : fal...

代码如下:


(function () {
 var fullscreenapi = {
  supportsfullscreen : false,
  isfullscreen : function () {
   return false;
  },
  requestfullscreen : function () {},
  cancelfullscreen : function () {},
  fullscreeneventname : '',
  prefix : ''
 },
 browserprefixes = 'webkit moz o ms khtml'.split(' ');

 

 // check for native support
 if (typeof document.cancelfullscreen != 'undefined') {
  fullscreenapi.supportsfullscreen = true;
 } else {
  // check for fullscreen support by vendor prefix
  for (var i = 0, il = browserprefixes.length; i < il; i++) {
   fullscreenapi.prefix = browserprefixes[i];

   if (typeof document[fullscreenapi.prefix + 'cancelfullscreen'] != 'undefined') {
    fullscreenapi.supportsfullscreen = true;

    break;
   }
  }
 }

 // update methods to do something useful
 if (fullscreenapi.supportsfullscreen) {
  fullscreenapi.fullscreeneventname = fullscreenapi.prefix + 'fullscreenchange';

  fullscreenapi.isfullscreen = function () {
   switch (this.prefix) {
   case '':
    return document.fullscreen;
   case 'webkit':
    return document.webkitisfullscreen;
   default:
    return document[this.prefix + 'fullscreen'];
   }
  }
  fullscreenapi.requestfullscreen = function (el) {
   return (this.prefix === '') ? el.requestfullscreen() : el[this.prefix + 'requestfullscreen']();
  }
  fullscreenapi.cancelfullscreen = function (el) {
   return (this.prefix === '') ? document.cancelfullscreen() : document[this.prefix + 'cancelfullscreen']();
  }
 }

 // jquery plugin
 if (typeof jquery != 'undefined') {
  jquery.fn.requestfullscreen = function () {

   return this.each(function () {
    if (fullscreenapi.supportsfullscreen) {
     fullscreenapi.requestfullscreen(this);
    }
   });
  };
 }

 // export api
 window.fullscreenapi = fullscreenapi;
})();

$(function(){
 $("#fullscreenbtn").click(function(){
  $("#fullscreen").requestfullscreen();
 });

 if(window.top != self){
  $("#tip").text("iframe里面不能演示该功能!请点击右下角的全屏查看!").show();
 }
});

if (!fullscreenapi.supportsfullscreen) {
 alert("您的破不支持全屏api哦,请换高版本的chrome或者firebox!");
}

 

 

. 代码如下:


<!doctype html>
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>javascript启用全屏</title>

 <script id="jquery_183" type="text/javascript" class="library" src=jquery-1.8.3.min.js"></script>
 </head>
 <body>
  <button id="fullscreenbtn">点击我进入全屏模式</button>
  <p id="fullscreen" class="fullscreen">
    <h1>
     我是全屏区域的内容!
   </h1>
   <p id="tip" style="display:none;">

   </p>
  </p>
 </body>
</html>

 

 

. 代码如下:


body{
background:#fff;
}
button{
 border:1px solid #ccc;
 cursor:pointer;
  display:block;
  margin:auto;
  position:relative;
  top:100px;
}

 

.fullscreen{
 padding-top:10%;
 text-align:center;
 background: none repeat scroll 0 0 #ffffff;
}

/* mozilla proposal (dash) */
.fullscreen:full-screen {
    width:100%;
    height:100%;
}

/* w3c proposal (no dash) */
.fullscreen:fullscreen {
    width:100%;
    height:100%;
}

/* currently working vendor prefixes */
.fullscreen:-webkit-full-screen, .fullscreen:-moz-full-screen {
    width:100%;
    height:100%;
}
:-webkit-full-screen{
width:100%;
    height:100%;
}