涉及方法:css
htmlhtml
<li id="js_full_screen"><span class="iconfont-full-screen"></span></li>
jsweb
// 全屏 var $fullScreenEle = $('#js_full_screen'), $fsChildEle = $fullScreenEle.children(), $navbarEle = $('.zh-navbar'), $headerEle = $('.zh-header'); $fullScreenEle.click(function() { if($fsChildEle.hasClass('iconfont-full-screen')) { // 全屏 var docEle = document.documentElement; var rfs = docEle.requestFullScreen || docEle.webkitRequestFullScreen || docEle.mozRequestFullScreen || docEle.msRequestFullScreen; if (rfs) { rfs.call(docEle); } else if (typeof window.ActiveXObject !== "undefined") { var wscript = new ActiveXObject("WScript.Shell"); if (wscript != null) { wscript.SendKeys("{F11}"); } } $fsChildEle.removeClass('iconfont-full-screen').addClass('iconfont-exit-screen'); $navbarEle.hide(); $headerEle.css('top', 10); } else { // 退出全屏 var cfs = document.cancelFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen || document.exitFullScreen; if (cfs) { cfs.call(document); } else if (typeof window.ActiveXObject !== "undefined") { var wscript = new ActiveXObject("WScript.Shell"); if (wscript != null) { wscript.SendKeys("{F11}"); } } } }); // 經過resize事件監聽退出全屏 $(window).resize(function() { var prevWinHgt = window.sessionStorage.getItem('winHgt'); if(prevWinHgt) { if(window.innerHeight<prevWinHgt && $fsChildEle.hasClass('iconfont-exit-screen')) { $fsChildEle.removeClass('iconfont-exit-screen').addClass('iconfont-full-screen'); $navbarEle.show(); $headerEle.css('top', 60); } } window.sessionStorage.setItem('winHgt', window.innerHeight); });
也可經過fullscreenchange來監控全屏變化session
document.addEventListener("fullscreenchange", function(e) { console.log("fullscreenchange", e); }); document.addEventListener("mozfullscreenchange", function(e) { console.log("mozfullscreenchange ", e); }); document.addEventListener("webkitfullscreenchange", function(e) { console.log("webkitfullscreenchange", e); }); document.addEventListener("msfullscreenchange", function(e) { console.log("msfullscreenchange", e); });