纯JS解决内嵌iframe全屏(兼容IE/火狐/谷歌)

遇到一些政府/国企开发时经常遇到兼容IE的问题,给大家介绍一种我刚研究出的一种解决方案

1.代码

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8">
    <script type="text/javascript" src="./../jquery/jquery-1.11.3.min.js"></script>
        <title>fullScreen</title>
        <style>
            body {
                margin: 0;
            }
        </style>
    
    
 <script type="text/javascript">
 
 $(document).keydown(function(event){
	    if(event.keyCode == 27){
	      //这里写你原始的iframe的大小
	      //例如
		 document.getElementById("fullScreen").style.height="500px";
         document.getElementById("fullScreen").style.width="500px";
	    }
	  });
 
 
 function showFullScreen(){
	             var elm = document.getElementById("fullScreen");
	             launchFullscreen(elm );
	         }      
	         // 全屏,退出按esc或参考参考参考注释代码写退出全屏按钮
	         function launchFullscreen(element) {
	         	
	             if (element.requestFullscreen) {
	                 element.requestFullscreen();
	             } else if (element.mozRequestFullScreen) {
	                 element.mozRequestFullScreen();//火狐
	             } else if (element.msRequestFullscreen) {
	                 element.msRequestFullscreen();//ie浏览器
	                 document.getElementById("fullScreen").style.height=window.screen.height+"px";
	                 document.getElementById("fullScreen").style.width=document.documentElement.clientWidth+"px";
	             } else if (element.webkitRequestFullscreen) {
	                 element.webkitRequestFullScreen();//谷歌浏览器
	             }
	         }
 </script>
    </head>
<body>

    <iframe id="fullScreen" allowfullscreen src="www.baidu.com"  
       frameborder="0" style="width: 500px;height: 500px;background:#aaa">
    </iframe>
    <button onclick="showFullScreen();">全屏</button>
</body>

 

解决思路:

1.全屏

通过判断出各个浏览器内核的不同分别给出不同的解决方案

2.退出全屏时

通过监控ESC健,动态设置iframe的大小(主要是为了兼容IE,谷歌等成熟浏览器会自动处理)

 

ps

1.在子页面也要写监控ESC事件

2.要在子页面设置 resize事件,常识了对吧(为了防止极端事件)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值