iWeb iFrames和平滑滚动问题

问题描述:

我正在使用iWeb(可怕的,但我必须使用它,长故事)。iWeb iFrames和平滑滚动问题

我已经设法在页面上获得一些平滑的滚动链接,但我无法滚动到正确的位置。

这里是我的被装载到一个iframe(FYI这是菜单)中的“HTML控件”代码:

<html> 
<head> 
<script type="text/javascript"> 

// the var's are referneces to iWeb's generated div's have to publish and get id's 
    var about  = "id1"; 
    var products = "id4"; 
    var technical = "id7"; 
// var contactus = "id14"; 

$(function() { 
    $('a[href*="#"]:not([href="#"])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 

     if (target.length) { 
     $('html, body', window.parent.document).animate({ 
      //scrollTop: parent.document.getElementById(products).offsetTop // this works but is a static ref 
      scrollTop: parent.document.getElementById(theTarget).offsetTop 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 

</script> 
</head> 
<body> 
    <div style="width: "100%"; height: "0%""> 
    <a href="#about" id="about" class="myButton">About</a> 
    <a href="#products" id="products" class="myButton">Products</a> 
    <a href="#technical" id="technical" class="myButton">Technical</a> 
    <a href="#contactus" id="contactus" class="myButton">Contact</a> 
    </div> 
</body> 
</html> 

现在,当我一个链接查看此一点击,它只会加载iframe页面而不是滚动主窗口。

但是,如果我评论/取消注释其他ScrollTop行它将工作,但显然它只会滚动到父窗口中的“id4”div。

我怎样才能得到这个工作方式我需要它没有复制/粘贴相同的功能反复每个链接?

我建议这类词典的来的链接散列映射元素ID:

var map = { 
    '#about':  'id1', 
    '#products': 'id4', 
    '#technical': 'id7', 
    '#contactus': 'id14', 
}; 

这样,那么你可以使用target作为重点在于地图:

if (target.length && target in map) { 
    $('html, body', window.parent.document).animate({ 
     scrollTop: parent.document.getElementById(map[target]).offsetTop, 
    }, 1000); 
    return false; 
} 

那是梦幻般的,它起初并不完全工作,但这里是我已经用来完全按照预期工作的代码:

<script type="text/javascript"> 
var map = { 
    'about':  'id1', 
    'products': 'id4', 
    'technical': 'id7', 
    'contactus': 'id11', 
}; 

$(function() { 
    $('a[href*="#"]:not([href="#"])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 

     var target = $(this.hash); 

     if (target.length) { 
     $('html, body', window.parent.document).animate({ 
      scrollTop: parent.document.getElementById(map[this.hash.slice(1)]).offsetTop 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 
</script>