JS实现全屏和退出全屏

直接上代码:

//innerHTML指的是从对象的起始位置到终止位置的全部内容,包括Html标签。
//innerText 指的是从起始位置到终止位置的内容,但它去除Html标签。

<script type="text/javascript">

function FullScreen(el){
var isFullscreen=document.fullScreen||document.mozFullScreen||document.webkitIsFullScreen;
if(!isFullscreen){//进入全屏,多重短路表达式
(el.requestFullscreen&&el.requestFullscreen())||
(el.mozRequestFullScreen&&el.mozRequestFullScreen())||
(el.webkitRequestFullscreen&&el.webkitRequestFullscreen())||(el.msRequestFullscreen&&el.msRequestFullscreen());

}else{ //退出全屏,三目运算符
document.exitFullscreen?document.exitFullscreen():
document.mozCancelFullScreen?document.mozCancelFullScreen():
document.webkitExitFullscreen?document.webkitExitFullscreen():'';
}
}
function toggleFullScreen(e){
var el=e.srcElement||e.target;//target兼容Firefox
el.innerHTML=='点我全屏'?el.innerHTML='退出全屏':el.innerHTML='点我全屏';
FullScreen(el);
}
</script>
<button onclick="toggleFullScreen(event)">点我全屏</button>

 

posted @ 2017-08-28 23:14  lyls  阅读(24176)  评论(1编辑  收藏  举报