欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery中弹出iframe内嵌页面元素到父页面并全屏化的实例代码

程序员文章站 2022-04-19 09:09:05
iframe和弹窗这些词对于js高手来说都是耳熟能详的东西,作为一个新人来说,还在学习阶段的我就在工作中遇到这么一个奇葩的需求,要在引入的iframe页面里做一个全屏化的功...

iframe和弹窗这些词对于js高手来说都是耳熟能详的东西,作为一个新人来说,还在学习阶段的我就在工作中遇到这么一个奇葩的需求,要在引入的iframe页面里做一个全屏化的功能.

粗略一看,这还不容易,模拟下f11的功能键什么的,于是网上一搜还真有一大堆关于全屏化的案例,遂借来用之.

然后高高兴兴的拿一个没有iframe引入的页面做了个测试页面查看全屏化功能效果,代码如下(fullscreenpage.html):

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>control tower</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body style="margin: 0px;height: 100%;width: 100%;">
 <div id="buttonpanel" style="position: absolute;left: 25%;z-index:100">
 <input id="full_screen_open" type="button" value="打开全屏">
 <input id="full_screen_close" type="button" value="退出全屏" style="display: none">
 </div>
 <div id="container" style="display:table;height: 50%;width: 50%;background-color: #004981;position:absolute;left: 25%;">
 <div style="display:table-cell;height: 50%;width: 50%;text-align: center;vertical-align: middle;border: 2px solid #dddddd;">
  <font id="font" size="30"></font>
 </div>
 </div>
</body>
<script src="./scripts/jquery/jquery-1.11.3.js" type="text/javascript"></script>
<script type="text/javascript">
$("#full_screen_open").on("click",function(){
 requestfullscreen($("#container")['0']);
 $("#font").empty();
 $("#font").text("已打开全屏化");
});
var requestfullscreen = function(element) {
 var requestmethod = element.requestfullscreen || element.webkitrequestfullscreen || element.mozrequestfullscreen || element.msrequestfullscreen;
 if (requestmethod) {
 requestmethod.call(element);
 } else if (typeof window.activexobject !== "undefined") {
 var wscript = new activexobject("wscript.shell");
 if (wscript !== null) {
  wscript.sendkeys("{f11}");
 }
 }
}
</script>
</html>

嗯,我自己觉得这个效果真的是不要太棒了,还做了浏览器兼容(firefox=mozrequestfullscreen;w3c=requestfullscreen;chrome等=webkitrequestfullscreen;ie11=msrequestfullscreen).....

于是,我立马放到项目里,结果是什么样子呢?执行下面的代码(parentpage.html)就知道了....

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>control tower</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body style="margin: 0px;height: 100%;width: 100%;">
 <div id="parentcontainer" style="height: 75%;width: 75%;position:absolute;left: 12.5%;border: 2px solid red;">
 <!-- 蓝色边框以内的内容是引入的iframe页面内容,也是需要做全屏化功能的页面 -->
 <iframe src="fullscreenpage.html" style="border: 2px solid blue;height: 100%;width: 100%;"></iframe>
 </div>
</body>
</html>

哦豁,好像没生效,那么为什么呢?

很明显没有起作用,那么怎么办呢?既然引入的子页面iframe不生效,是不是从父页面或许就可以了?

那就赶紧试试找到父类并执行全屏功能,把页面(fullscreenpage.html)改一改,代码如下:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>control tower</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body style="margin: 0px;height: 100%;width: 100%;">
 <div id="buttonpanel" style="position: absolute;left: 25%;z-index:100">
 <input id="full_screen_open" type="button" value="打开全屏">
 <input id="full_screen_close" type="button" value="退出全屏" style="display: none">
 </div>
 <div id="container" style="display:table;height: 50%;width: 50%;background-color: #004981;position:absolute;left: 25%;">
 <div style="display:table-cell;height: 50%;width: 50%;text-align: center;vertical-align: middle;border: 2px solid #dddddd;">
  <font id="font" size="30"></font>
 </div>
 </div>
</body>
<script src="./scripts/jquery/jquery-1.11.3.js" type="text/javascript"></script>
<script type="text/javascript">
$("#full_screen_open").on("click",function(){
 /* 获取父类的document */
 var parentdoc = parent.document;
 /* 定义一个接收元素的变量 */
 var thisiframe = null;
 /* 用jquery遍历父类的所有iframe,找到我引入的那个iframe,
  假设我不知道是哪个页面要引入我的iframe,但是引入我的iframe的src肯定会有引入这个页面的名字,
  所以通过这个去检索,一定能找到引入这个页面的iframe,然后把这个iframe的元素全屏化也就是把原来的页面全屏化 */
 $("iframe",window.parent.document).each(function(index,e){
 if (e.src.indexof("fullscreenpage.html") > 0) {
  thisiframe = e;
  return false;
 }
 });
 requestfullscreen(thisiframe);
 $("#font").empty();
 $("#font").text("已打开全屏化");
});
var requestfullscreen = function(element) {
 var requestmethod = element.requestfullscreen || element.webkitrequestfullscreen || element.mozrequestfullscreen || element.msrequestfullscreen;
 if (requestmethod) {
 requestmethod.call(element);
 } else if (typeof window.activexobject !== "undefined") {
 var wscript = new activexobject("wscript.shell");
 if (wscript !== null) {
  wscript.sendkeys("{f11}");
 }
 }
}
</script>
</html>

哈哈,改了之后发现果然可以了,问题解决。

jquery还请自行下载并导入引用,我这里就不细说了.

这里分享一个jquery下载的地址:jquery下载所有版本(实时更新)

以上所述是小编给大家介绍的jquery中弹出iframe内嵌页面元素到父页面并全屏化的实现代码,希望对大家有所帮助