frame和彈跳這些字對於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;Chr ome等=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>
哈哈,改了之後發現果然可以了,問題解決。
以上是彈出iframe內嵌頁面元素到父頁面並全螢幕化測試(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!