js 点击实现全屏 类似F11效果

方法一
style

<style>
        /* Basic element styles */
        html {
            color: #000;
            background: paleturquoise;
            min-height: 100%;
        }

        /* Structure */
        .container {
            text-align: center;
            width: 500px;
            min-height: 600px;
            background: #fff;
            border: 1px solid #ccc;
            border-top: none;
            margin: 20px auto;
            padding: 20px;
            border-radius: 10px;
            -moz-border-radius: 10px;
            -webkit-border-radius: 10px;
            box-shadow: 1px 1px 10px #000;
            -moz-box-shadow: 1px 1px 10px #000;
            -webkit-box-shadow: 1px 1px 5px #000;
        }

        button {
            margin: 200px auto;
            width: 100px;
            height: 30px;
            background-color: aliceblue;
        }

        /* Fullscreen */
        html:-moz-full-screen {
            background: blue;
        }

        html:-webkit-full-screen {
            background: blue;
        }

        html:-ms-fullscreen {
            background: blue;
            width: 100%;
            /* needed to center contents in IE */
        }

        html:fullscreen {
            background: blue;
        }
    </style>

html

<div class="container" <button id="full-screen">全屏</button>
        <button id="exit-fullscreen">退出</button>
 </div>

JS

(function () {
            var viewFullScreen = document.getElementById("full-screen");
            if (viewFullScreen) {
                viewFullScreen.addEventListener("click", function () {
                    var docElm = document.documentElement;
                    if (docElm.requestFullscreen) {
                        docElm.requestFullscreen();
                    }
                    else if (docElm.msRequestFullscreen) {
                        docElm.msRequestFullscreen();
                    }
                    else if (docElm.mozRequestFullScreen) {
                        docElm.mozRequestFullScreen();
                    }
                    else if (docElm.webkitRequestFullScreen) {
                        docElm.webkitRequestFullScreen();
                    }
                }, false);
            }

            var cancelFullScreen = document.getElementById("exit-fullscreen");
            if (cancelFullScreen) {
                cancelFullScreen.addEventListener("click", function () {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    }
                    else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                    else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    }
                    else if (document.webkitCancelFullScreen) {
                        document.webkitCancelFullScreen();
                    }
                }, false);
            }


            var fullscreenState = document.getElementById("fullscreen-state");
            if (fullscreenState) {
                document.addEventListener("fullscreenchange", function () {
                    fullscreenState.innerHTML = (document.fullscreenElement) ? "" : "not ";
                }, false);

                document.addEventListener("msfullscreenchange", function () {
                    fullscreenState.innerHTML = (document.msFullscreenElement) ? "" : "not ";
                }, false);

                document.addEventListener("mozfullscreenchange", function () {
                    fullscreenState.innerHTML = (document.mozFullScreen) ? "" : "not ";
                }, false);

                document.addEventListener("webkitfullscreenchange", function () {
                    fullscreenState.innerHTML = (document.webkitIsFullScreen) ? "" : "not ";
                }, false);
            }

        })();

方法二

html

<div style="margin:0 auto;height:600px;width:700px;">
        <button id="btn">全屏</button>
        <div id="content" style="margin:0 auto;height:500px;width:700px; background:#ccc;">
            <h1>全屏展示和退出全屏</h1>
        </div>
</div>

JS

document.getElementById("btn").onclick = function () {
            var elem = document.getElementById("content");
            requestFullScreen(elem);
            /*
             注意这里的样式的设置表示全屏显示之后的样式,
             退出全屏后样式还在,
             若要回到原来样式,需在退出全屏里把样式还原回去
             (见写法三)
             */
            elem.style.height = '800px';
            elem.style.width = '1000px';
        };
        function requestFullScreen(element) {
            var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
            if (requestMethod) {
                requestMethod.call(element);
            } else if (typeof window.ActiveXObject !== "undefined") {
                var wscript = new ActiveXObject("WScript.Shell");
                if (wscript !== null) {
                    wscript.SendKeys("{F11}");
                }
            }
        }

方法三

html

<div style="margin:0 auto;height:600px;width:700px;">
 <div id="content" style="margin:0 auto;height:500px;width:700px; background:#ccc;" >
  <button id="btn">全屏</button>
  <h1>全屏展示和退出全屏</h1>
  <button id="btnn" >退出</button>
 </div>
</div>

JS

document.getElementById("btn").onclick=function(){
  var elem = document.getElementById("content");
  requestFullScreen(elem);
  /*
   注意这里的样式的设置表示全屏显示之后的样式,
   退出全屏后样式还在,
   若要回到原来样式,需在退出全屏里把样式还原回去
   */
  elem.style.height = '800px';
  elem.style.width = '1000px';
 };
document.getElementById("btnn").onclick=function () {
  exitFullscreen();
 };
 /*
  全屏显示
  */
function requestFullScreen(element) {
 var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
 requestMethod.call(element);
 };
 /*
  全屏退出
  */
function exitFullscreen() {
  var elem = document;
  var elemd = document.getElementById("content");
  if (elem.webkitCancelFullScreen) {
   elem.webkitCancelFullScreen();
  } else if (elem.mozCancelFullScreen) {
   elemd.mozCancelFullScreen();
  } else if (elem.cancelFullScreen) {
   elem.cancelFullScreen();
  } else if (elem.exitFullscreen) {
   elem.exitFullscreen();
  } else {
   //浏览器不支持全屏API或已被禁用
  };
 
  /*
   退出全屏后样式还原
   */
  elemd.style.height = '500px';
  elemd.style.width = '700px'
 
 }

方法四 JQUERY
html

<div id="cont" STYLE="width: 500px;height: 300px;background-color: aliceblue;margin: auto">
 <button id="btn">全屏&退出</button>
</div>

CSS

.full{
  position: fixed;
  align-content: center;
  /*top: 10px;*/
  /*left: 10px;*/
  /*
   原来基础的百分百
  */
  width: 100%;
  height: 100%;
  overflow: auto;
 }

fullScreen.js

(function ($) {
 
 // Adding a new test to the jQuery support object
 $.support.fullscreen = supportFullScreen();
 
 // Creating the plugin
 $.fn.fullScreen = function (props) {
 
  if (!$.support.fullscreen || this.length != 1) {
 
   // The plugin can be called only
   // on one element at a time
 
   return this;
  }
 
  if (fullScreenStatus()) {
   // if we are already in fullscreen, exit
   cancelFullScreen();
   return this;
  }
 
  // You can potentially pas two arguments a color
  // for the background and a callback function
 
  var options = $.extend({
   'background': '#111',
   'callback': function () {}
  }, props);
 
  // This temporary div is the element that is
  // actually going to be enlarged in full screen
 
  var fs = $('<div>', {
   'css': {
    'overflow-y': 'auto',
    'background': options.background,
    'width': '100%',
    'height': '100%',
    'align': 'center'
   }
  });
 
  var elem = this;
 
  // You can use the .fullScreen class to
  // apply styling to your element
  elem.addClass('fullScreen');
 
  // Inserting our element in the temporary
  // div, after which we zoom it in fullscreen
  fs.insertBefore(elem);
  fs.append(elem);
  requestFullScreen(fs.get(0));
 
  fs.click(function (e) {
   if (e.target == this) {
    // If the black bar was clicked
    cancelFullScreen();
   }
  });
 
  elem.cancel = function () {
   cancelFullScreen();
   return elem;
  };
 
  onFullScreenEvent(function (fullScreen) {
 
   if (!fullScreen) {
 
    // We have exited full screen.
    // Remove the class and destroy
    // the temporary div
 
    elem.removeClass('fullScreen').insertBefore(fs);
    fs.remove();
   }
 
   // Calling the user supplied callback
   options.callback(fullScreen);
  });
 
  return elem;
 };
 
 
 // These helper functions available only to our plugin scope.
 
 
 function supportFullScreen() {
  var doc = document.documentElement;
 
  return ('requestFullscreen' in doc) ||
   ('mozRequestFullScreen' in doc && document.mozFullScreenEnabled) ||
   ('webkitRequestFullScreen' in doc);
 }
 
 function requestFullScreen(elem) {
  if (elem.requestFullscreen) {
   elem.requestFullscreen();
  } else if (elem.mozRequestFullScreen) {
   elem.mozRequestFullScreen();
  } else if (elem.webkitRequestFullScreen) {
   elem.webkitRequestFullScreen();
  }
 }
 
 function fullScreenStatus() {
  return document.fullscreen ||
   document.mozFullScreen ||
   document.webkitIsFullScreen;
 }
 
 function cancelFullScreen() {
  if (document.exitFullscreen) {
   document.exitFullscreen();
  } else if (document.mozCancelFullScreen) {
   document.mozCancelFullScreen();
  } else if (document.webkitCancelFullScreen) {
   document.webkitCancelFullScreen();
  }
 }
 
 function onFullScreenEvent(callback) {
  $(document).on("fullscreenchange mozfullscreenchange webkitfullscreenchange", function () {
   // The full screen status is automatically
   // passed to our callback as an argument.
   callback(fullScreenStatus());
  });
 }
 
})(jQuery);

调用

$(function () {
 $("#btn").click(function () {
  $("#cont").fullScreen();
 })
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,558评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,002评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,024评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,144评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,255评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,295评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,068评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,478评论 1 305
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,789评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,965评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,649评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,267评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,982评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,800评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,847评论 2 351