彈出iframe內嵌頁面元素到父頁面並全屏化


(注冊博客好久了,一直沒舍得添磚加瓦,主要是每次想寫點東西的時候,隨便搜一搜發現都比我總結的都要好,甚感尷尬,但是總是要開始的,所以這就是我的第一篇博客,也絕不會是最后一篇,廢話不多說,直接入正題)

 

iframe和彈窗這些詞對於js高手來說都是耳熟能詳的東西,作為一個新人來說,還在學習階段的我就在工作中遇到這么一個奇葩的需求,要在引入的iframe頁面里做一個全屏化的功能.

粗略一看,這還不容易,模擬下F11的功能鍵什么的,於是網上一搜還真有一大堆關於全屏化的案例,遂借來用之.

然后高高興興的拿一個沒有iframe引入的頁面做了個測試頁面查看全屏化功能效果,代碼如下(fullScreenPage.html):

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title>Control Tower</title>
 5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 6 </head>
 7 <body style="margin: 0px;height: 100%;width: 100%;">
 8     <div id="buttonPanel" style="position: absolute;left: 25%;z-index:100">
 9         <input id="full_screen_open" type="button" value="打開全屏">
10         <input id="full_screen_close" type="button" value="退出全屏" style="display: none">
11     </div>
12     <div id="container" style="display:table;height: 50%;width: 50%;background-color: #004981;position:absolute;left: 25%;">
13         <div style="display:table-cell;height: 50%;width: 50%;text-align: center;vertical-align: middle;border: 2px solid #DDDDDD;">
14             <font id="font" size="30"></font>
15         </div>
16     </div>
17 </body>
18 <script src="./scripts/jquery/jquery-1.11.3.js" type="text/javascript"></script>
19 <script type="text/javascript">
20 $("#full_screen_open").on("click",function(){
21     requestFullScreen($("#container")['0']);
22     $("#font").empty();
23     $("#font").text("已打開全屏化");
24 });
25 var requestFullScreen = function(element) {
26     var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
27     if (requestMethod) {
28         requestMethod.call(element);
29     } else if (typeof window.ActiveXObject !== "undefined") {
30         var wscript = new ActiveXObject("WScript.Shell");
31         if (wscript !== null) {
32             wscript.SendKeys("{F11}");
33         }
34     }
35 }
36 </script>
37 </html>

嗯,我自己覺得這個效果真的是不要太棒了,還做了瀏覽器兼容(FireFox=mozRequestFullScreen;W3C=requestFullscreen;Chrome等=webkitRequestFullScreen;ie11=msRequestFullscreen).....

於是,我立馬放到項目里,結果是什么樣子呢?執行下面的代碼(parentPage.html)就知道了....

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title>Control Tower</title>
 5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 6 </head>
 7 <body style="margin: 0px;height: 100%;width: 100%;">
 8     <div id="parentContainer" style="height: 75%;width: 75%;position:absolute;left: 12.5%;border: 2px solid red;">
 9         <!-- 藍色邊框以內的內容是引入的iframe頁面內容,也是需要做全屏化功能的頁面 -->
10         <iframe src="fullScreenPage.html" style="border: 2px solid blue;height: 100%;width: 100%;"></iframe>
11     </div>
12 </body>
13 </html>

哦豁,好像沒生效,那么為什么呢?

很明顯沒有起作用,那么怎么辦呢?既然引入的子頁面iframe不生效,是不是從父頁面或許就可以了?

那就趕緊試試找到父類並執行全屏功能,把頁面(fullScreenPage.html)改一改,代碼如下:

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title>Control Tower</title>
 5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 6 </head>
 7 <body style="margin: 0px;height: 100%;width: 100%;">
 8     <div id="buttonPanel" style="position: absolute;left: 25%;z-index:100">
 9         <input id="full_screen_open" type="button" value="打開全屏">
10         <input id="full_screen_close" type="button" value="退出全屏" style="display: none">
11     </div>
12     <div id="container" style="display:table;height: 50%;width: 50%;background-color: #004981;position:absolute;left: 25%;">
13         <div style="display:table-cell;height: 50%;width: 50%;text-align: center;vertical-align: middle;border: 2px solid #DDDDDD;">
14             <font id="font" size="30"></font>
15         </div>
16     </div>
17 </body>
18 <script src="./scripts/jquery/jquery-1.11.3.js" type="text/javascript"></script>
19 <script type="text/javascript">
20 $("#full_screen_open").on("click",function(){
21     /* 獲取父類的document */
22     var parentDoc = parent.document;
23     /* 定義一個接收元素的變量 */
24     var thisIframe = null;
25     /* 用jQuery遍歷父類的所有iframe,找到我引入的那個iframe,
26           假設我不知道是哪個頁面要引入我的iframe,但是引入我的iframe的src肯定會有引入這個頁面的名字,
27           所以通過這個去檢索,一定能找到引入這個頁面的iframe,然后把這個iframe的元素全屏化也就是把原來的頁面全屏化 */
28     $("iframe",window.parent.document).each(function(index,e){
29         if (e.src.indexOf("fullScreenPage.html") > 0) {
30             thisIframe = e;
31             return false;
32         }
33     });
34     requestFullScreen(thisIframe);
35     $("#font").empty();
36     $("#font").text("已打開全屏化");
37 });
38 var requestFullScreen = function(element) {
39     var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
40     if (requestMethod) {
41         requestMethod.call(element);
42     } else if (typeof window.ActiveXObject !== "undefined") {
43         var wscript = new ActiveXObject("WScript.Shell");
44         if (wscript !== null) {
45             wscript.SendKeys("{F11}");
46         }
47     }
48 }
49 </script>
50 </html>

 

哈哈,改了之后發現果然可以了,問題解決。

jQuery還請自行下載並導入引用,我這里就不細說了.

這里分享一個jQuery下載的地址:jquery下載所有版本(實時更新)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM