DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 用jquery修復在iframe下的頁面錨點失效問題
用jquery修復在iframe下的頁面錨點失效問題
編輯:JQuery特效代碼     

應用場景是:iframe頁面沒有滾動條,在父窗體中出現滾動條,錨點標記就會失效,因為,錨點是根據當前窗口滾動條滾動窗口的,成為子窗體後沒有了滾動條,自然不會滾動。

解決辦法是:用js判斷頁面是否被嵌套,用js計算iframe在父窗體位置,錨點在firame中的位置,兩者相加成為父窗體的滾動。

遇到問題:獲取父窗體元素(因為有域限制,所有需要在網絡環境下方位(即http://domain.com));父窗體嵌套多個iframe,判斷是否是當前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則需要在網絡中訪問,因為js裡有域限制
//如果沒有iframe則不進行處理,
if(window!==window.top){
//獲取top窗口中的iframe,如果有iframe嵌套過多,請自行修改
var iframeList=window.top.document.getElementsByTagName('iframe');
for(var i=0;i<iframeList.length;i++){
//判斷當前窗口是否循環中的iframe
if(window.location.toString().indexOf(iframeList[i].getAttribute('src').toString())!=-1){

//等自己的所在iframe加載完成給a錨點加事件
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){//判斷是否是錨點而不是鏈接
var name = href.substring(href.indexOf('#')+1);
$(this).bind('click',function(){
$('a').each(function(){
if($(this).attr('name')==name){
//父窗口滾動
$(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>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved