用jquery修復(fù)在iframe下的頁面錨點(diǎn)失效問題
應(yīng)用場景是:iframe頁面沒有滾動(dòng)條,在父窗體中出現(xiàn)滾動(dòng)條,錨點(diǎn)標(biāo)記就會(huì)失效,因?yàn)?,錨點(diǎn)是根據(jù)當(dāng)前窗口滾動(dòng)條滾動(dòng)窗口的,成為子窗體后沒有了滾動(dòng)條,自然不會(huì)滾動(dòng)。
解決辦法是:用js判斷頁面是否被嵌套,用js計(jì)算iframe在父窗體位置,錨點(diǎn)在firame中的位置,兩者相加成為父窗體的滾動(dòng)。
遇到問題:獲取父窗體元素(因?yàn)橛杏蛳拗?,所有需要在網(wǎng)絡(luò)環(huán)境下方位(即http://domain.com));父窗體嵌套多個(gè)iframe,判斷是否是當(dāng)前iframe頁面。
代碼:
父窗體頁面 index.html
<!doctype html> <html> <head> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; border: 0; } html, body{ width: 100%; height: 100%; } </style> </head> <body> <div style="width:100%;background:#f00;height:500px;"></div> <a href="">dd</a> <a href="">ddd</a> <iframe name="iframe2" id="iframe2" src="iframe.html?a=b&c=d" style="width:100%;height:2060px;"></iframe> <iframe name="iframe2" id="iframe2" src="iframe.html?a=d&c=b" style="width:100%;height:2060px;"></iframe> </body> </html>
子窗體頁面iframe.html
<!doctype html> <html> <head> <title></title> <style type="text/css"> a{ padding: 5px; border: 1px solid #f00; float: left; display: block; margin-right: 5px; } div{ width: 80%; margin: 10px auto; height: 500px; border: 1px solid #f00; font-size: 30px; } </style> <script type="text/javascript" src="jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(function(){ //如果是iframe則需要在網(wǎng)絡(luò)中訪問,因?yàn)閖s里有域限制 //如果沒有iframe則不進(jìn)行處理, if(window!==window.top){ //獲取top窗口中的iframe,如果有iframe嵌套過多,請(qǐng)自行修改 var iframeList=window.top.document.getElementsByTagName('iframe'); for(var i=0;i<iframeList.length;i++){ //判斷當(dāng)前窗口是否循環(huán)中的iframe if(window.location.toString().indexOf(iframeList[i].getAttribute('src').toString())!=-1){ //等自己的所在iframe加載完成給a錨點(diǎn)加事件 window.top.document.getElementsByTagName('iframe')[i].onload=function(){ //確定iframe在父窗體的距頂部距離 var top = window.top.document.getElementsByTagName('iframe')[i].offsetTop; $('a').each(function(){ var href = $(this).attr('href'); if(href.indexOf('#')!=-1){//判斷是否是錨點(diǎn)而不是鏈接 var name = href.substring(href.indexOf('#')+1); $(this).bind('click',function(){ $('a').each(function(){ if($(this).attr('name')==name){ //父窗口滾動(dòng) $(window.parent).scrollTop($(this).offset().top+top); } }); }); } }); } } } } }); </script> </head> <body> <a href="#a" rel="external nofollow" >a</a> <a href="#b" rel="external nofollow" >b</a> <a href="#c" rel="external nofollow" >c</a> <a href="#d" rel="external nofollow" >d</a> <div><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" a">A</a></div> <div><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" b">B</a></div> <div><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" c">C</a></div> <div><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" d">D</a></div> </body> </html>
相關(guān)文章
jQuery實(shí)現(xiàn)指定內(nèi)容滾動(dòng)同時(shí)左側(cè)或其它地方不滾動(dòng)的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)指定內(nèi)容滾動(dòng)同時(shí)左側(cè)或其它地方不滾動(dòng)的方法,可實(shí)現(xiàn)頁面滾動(dòng)的時(shí)候指定位置懸浮固定的效果,涉及jquery針對(duì)頁面元素屬性的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2015-08-08jquery實(shí)現(xiàn)漂浮在網(wǎng)頁右側(cè)的qq在線客服插件示例
很實(shí)用的一款QQ在線客服代碼,點(diǎn)擊QQ圖標(biāo),可直接與客服對(duì)話,詳細(xì)如下,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-05-05jQuery里filter()函數(shù)與find()函數(shù)用法分析
這篇文章主要介紹了jQuery里filter()函數(shù)與find()函數(shù)用法,實(shí)例對(duì)比分析了filter()函數(shù)與find()函數(shù)的功能與相關(guān)使用技巧,需要的朋友可以參考下2015-06-06easyui 中的datagrid跨頁勾選問題的實(shí)現(xiàn)方法
很多朋友都遇到這樣的需求,easyui的datagrid分頁顯示數(shù)據(jù),如果有需求要求勾選多條數(shù)據(jù)且不再同一頁中,easyui會(huì)保存在其他頁選中的數(shù)據(jù)嗎?小編結(jié)合資料自己整理了一篇文章,需要的的朋友參考下吧2017-01-01基于jQuery創(chuàng)建鼠標(biāo)懸停效果的方法
這篇文章主要介紹了基于jQuery創(chuàng)建鼠標(biāo)懸停效果的方法,實(shí)例分析了jQuery實(shí)現(xiàn)鼠標(biāo)特效的原理與詳細(xì)步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03基于jQuery的為attr添加id title等效果的實(shí)現(xiàn)代碼
下面的例子是通過jquery為連接增加title描述的代碼,在當(dāng)前頁的連接上寫上,你好,現(xiàn)在在試驗(yàn)連接文字的簡單描述。2011-04-04