用html5 js實現瀏覽器全屏

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.