全屏显示图像 - 使用JavaScript/HTML5

Jas*_*per 5 javascript html5 internet-explorer google-chrome

我在网页上有一张图片.当我点击它时,它应该显示全屏.

我有以下代码:

<!doctype html>
<html>
  <head>      
     <script>
        function makeFullScreen() {
         var divObj = document.getElementById("theImage");
       //Use the specification method before using prefixed versions
      if (divObj.requestFullscreen) {
        divObj.requestFullscreen();
      }
      else if (divObj.msRequestFullscreen) {
        divObj.msRequestFullscreen();            
      }
      else if (divObj.mozRequestFullScreen) {
        divObj.mozRequestFullScreen();
      }
      else if (divObj.webkitRequestFullscreen) {
        divObj.webkitRequestFullscreen();
      } else {
        console.log("Fullscreen API is not supported");
      } 

    }
     </script>

  </head>
  <body>
    Click Image to display Full Screen...</br>

    <img id="theImage" style="width:400px; height:auto;" src="pic1.jpg" onClick="makeFullScreen()"></img>

  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我面临的问题 - 在全屏模式下:
1.在所有浏览器上,图像以小尺寸显示(即与浏览器页面上的尺寸相同),而不是原始尺寸.
2.在IE中 - 图像不居中,它显示在全屏的左侧(为什么不居中).在Chrome中 - 它根据需要居中.

Jas*_*per 6

所以,经过一些试验后找到了解决方案......

解决方案位于样式部分:
- 宽度,高度,边距设置为"自动",
- 但也标记为" !important " - 这允许您覆盖网页上图像的内嵌样式 - 在全屏模式下.

<!doctype html>
<html>
  <head>
     <style>
      .fullscreen:-webkit-full-screen {
      width: auto !important;
      height: auto !important;
      margin:auto !important;
  }
     .fullscreen:-moz-full-screen {
      width: auto !important;
      height: auto !important;
      margin:auto !important;
  }
     .fullscreen:-ms-fullscreen {
      width: auto !important;
      height: auto !important;
      margin:auto !important;
  }     
     </style>
     <script>
        function makeFullScreen() {
         var divObj = document.getElementById("theImage");
       //Use the specification method before using prefixed versions
      if (divObj.requestFullscreen) {
        divObj.requestFullscreen();
      }
      else if (divObj.msRequestFullscreen) {
        divObj.msRequestFullscreen();               
      }
      else if (divObj.mozRequestFullScreen) {
        divObj.mozRequestFullScreen();      
      }
      else if (divObj.webkitRequestFullscreen) {
        divObj.webkitRequestFullscreen();       
      } else {
        console.log("Fullscreen API is not supported");
      } 

    }
     </script>

  </head>
  <body>
    Hello Image...</br>

    <img id="theImage" style="width:400px; height:auto;"  class="fullscreen" src="pic1.jpg" onClick="makeFullScreen()"></img>

  </body>
</html>
Run Code Online (Sandbox Code Playgroud)