function full(ele) { if (ele.requestFullscreen) { ele.requestFullscreen(); } else if (ele.mozRequestFullScreen) { ele.mozRequestFullScreen(); } else if (ele.webkitRequestFullscreen) { ele.webkitRequestFullscreen(); } else if (ele.msRequestFullscreen) { ele.msRequestFullscreen(); } }
ele
:要全屏的元素,能夠是document.body
也能夠是某一個div
思路:
1.判斷該瀏覽器是否具備requestFullscreen
方法
2.有,則直接執行ele.requestFullscreen()
;沒有則作瀏覽器兼容判斷。javascript
function exitFullscreen() { if(document.exitFullScreen) { document.exitFullScreen(); } else if(document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if(document.webkitExitFullscreen) { document.webkitExitFullscreen(); } else if(document.msExitFullscreen) { document.msExitFullscreen(); } }
退出全屏直接使用document
調用exitFullscreen
方法便可。css
function getFullscreenElement() { return ( document.fullscreenElement || document.mozFullScreenElement || document.msFullScreenElement || document.webkitFullscreenElement||null ); }
document.fullscreenElement()
:獲取當前全屏的元素。
假設id爲div1的Element當前爲全屏狀態則 document.querySelector("#div1")===document.fullscreenElement
java
function isFullScreen() { return !! ( document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen || document.webkitFullScreen || document.msFullScreen ); }
function isFullscreenEnabled() { return ( document.fullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled ); }
1.document
下沒有requestFullscreen
2.requestFullscreen
方法只能由用戶觸發,好比:在onload
事件中不能觸發
3.頁面跳轉需先退出全屏
4.進入全屏的元素,將脫離其父元素,因此可能致使以前某些css的失效
解決方案:使用 :full-screen
僞類 爲元素添加全屏時的樣式(使用時爲了兼容注意添加-webkit、-moz或-ms前綴)
5.一個元素A全屏後,其子元素要再全屏,需先讓元素A退出全屏web
單擊播放/暫停。瀏覽器
function onDocumentClick(event) {
clearTimeout(clickTimeId);
//執行延時
clickTimeId = setTimeout(function() {
//此處爲單擊事件要執行的代碼
if(video.paused){
video.play();
}else{
video.pause();
}
}, 250);
}
雙擊全屏/解除全屏
function onDocumenDblClick(event) {
clearTimeout(clickTimeId);
console.log("鼠標雙擊");
console.log(isFullScreen())
if(isFullScreen()){
exitFullscreen()
}else{
full(video)
}
}
隱藏vedio控件 css :
video::-webkit-media-controls{ display:none !important;}