js控制页面进入全屏退出全屏,非模拟f11,兼容目前所有浏览器

子成君 876 1

js全屏操作api

参考MDN全屏api文档:https://developer.mozilla.org/zh-CN/docs/Web/API/Fullscreen_API

网上搜索的大多是通过SendKeys的方式传入f11来模拟f11全屏操作,

可都2020年啦~js早就有支持全屏操作的api了

适配各大内核主流浏览器,代码:

          var docElm = document.documentElement
          //W3C
          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()
          }

关闭全屏操作,代码:

 //W3C
          if (document.exitFullscreen) {
            document.exitFullscreen()
          }
          //FireFox
          else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen()
          }
          //Chrome等
          else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen()
          }
          //IE11
          else if (document.msExitFullscreen) {
            document.msExitFullscreen()
          }

网站上的具体实现

具体效果查看网站右下角即可,f12仔细看控制全屏按钮的代码,不难发现有一个自定义属性

js控制页面进入全屏退出全屏,非模拟f11,兼容目前所有浏览器

data-full的值就是用来区别当前是全屏还是正常屏幕视口的参数

 

将上述两个代码块封装成一个方法:

      function requestFullScreen(ele) {
        let full = ele.data("full");
        if (full != "full") {
          var docElm = document.documentElement
          //W3C
          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()
          }
          ele.data("full","full");
        } else {
          //W3C
          if (document.exitFullscreen) {
            document.exitFullscreen()
          }
          //FireFox
          else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen()
          }
          //Chrome等
          else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen()
          }
          //IE11
          else if (document.msExitFullscreen) {
            document.msExitFullscreen()
          }
           ele.data("full","half");
        }
      }

直接使用即可,绑定按钮点击事件传入按钮的DOM节点

    $("#screen-control").on( "click", function() {
        requestFullScreen($("#screen-control"));
    } );

是不是很简单呐~

发表评论 取消回复
OwO 图片 链接 代码

  1. casino en ligne
    casino en ligne 超级小萌新

    Wow, marvelous blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your web site is fantastic, let alone the content!

    <a href="https://giteas.fullmooncyberworks.com/geraldoiue5164">casino en ligne</a>
    Thanks for finally talking about > js控制页面进入全屏退出全屏,非模拟f11,兼容目前所有浏览器
    | 前端打更人 < Loved it!
    <a href="https://bizlinks.work/caradix2106308">casino en ligne</a>
    Hi it's me, I am also visiting this web site on a regular basis,
    this site is actually fastidious and the visitors are in fact sharing pleasant thoughts.

    <a href="https://gitea.createk.pe/ygpmichael6389">casino en ligne fiable</a>
    If you wish for to increase your know-how just keep visiting
    this website and be updated with the newest news posted here.

    <a href="https://tippy-t.com/leilaevans9206">casino en ligne</a>
    I know this if off topic but I'm looking into starting my own blog and was wondering what all is needed
    to get setup? I'm assuming having a blog like yours would cost a
    pretty penny? I'm not very internet savvy so I'm not 100% positive.
    Any suggestions or advice would be greatly appreciated.
    Kudos
    <a href="https://www.meikeyun.com/indiana920661">casino en ligne</a>
    My spouse and I absolutely love your blog and find almost all of your post's to
    be just what I'm looking for. Do you offer guest writers to write content in your case?
    I wouldn't mind producing a post or elaborating on a few
    of the subjects you write regarding here. Again, awesome weblog!

    <a href="https://www.geoffroy-berry.fr/jessie73d2187">casino en ligne</a>
    Thanks , I've just been searching for information approximately this topic
    for a long time and yours is the best I have found out so far.
    But, what concerning the conclusion? Are you certain concerning the supply?

    <a href="http://www.career4.co.kr/bbs/board.php?bo_table=ci_consulting&wr_id=26">casino en ligne</a>
    Hi! Do you know if they make any plugins to assist with SEO?

    I'm trying to get my blog to rank for some targeted keywords but I'm not seeing
    very good success. If you know of any please share.
    Thank you!
    <A HREF=https://git.itbcode.com/louellacourtoi>casino en ligne fiable</A>
    Paragraph writing is also a excitement, if you be familiar with after that you can write otherwise it is
    complicated to write.
    <a href="https://jooble.az/employer/serveur-prive">casino en ligne</a>
    Thanks for sharing such a good thought, paragraph is good, thats why i have read it fully
    <A HREF=https://git.bloade.com/curtconsiden2>meilleur casino en ligne</A>

分享