我的菜单和基于#anchors 的导航有问题。我正在使用 JS 函数在滚动时将我的菜单置于顶部(#stickyheader)。我的菜单上有一个 div 在滚动时会消失(#unstickyheader)。这个 div 的高度不是固定的,它取决于它的内容长度和屏幕的宽度。我的菜单垂直链接到我的页面的不同部分,在我处理锚导航的 js 函数中,我有一个偏移值,以在我的菜单下使用定义的填充显示我的部分标题:
$target.offset().top-90
这是我的CSS:
html,body{padding:0;margin:0;font-family: Helvetica, Geneva, Arial sans-serif;font-size:16px;;color: black;line-height: 24px;color: black;text-transform: lowercase;letter-spacing: 1px;background-color: white}
#wrapper{margin-left: 30px;margin-right: 30px;padding-bottom: 20px;}
#entry{position: relative}
#stickyheader { width: 100%;padding-top:10px;top: 0;z-index: 1000;background-color: white;padding-bottom: 10px;line-height: 24px;}
#unstickyheader {height:auto;padding-top:20px;}
.page{min-height: 3000px}
.separation{height: 600px;
background-color: white;
width: 100%;
margin-top: 300px;
margin-bottom: 300px;}
我的 html :
<div id="wrapper">
<div id="unstickyheader">
<div class="bloc_bio">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas porttitor dignissim mollis. Nam tempor tristique ultricies. Nullam blandit aliquet augue, et elementum elit. Morbi at enim ut magna semper varius. Curabitur purus turpis, condimentum at ullamcorper vel, adipiscing vel enim. Phasellus hendrerit semper nisi quis molestie. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur egestas nibh nunc, ac facilisis dui ultricies eget. Integer vestibulum ultricies diam, ut laoreet risus. Morbi suscipit venenatis tortor sit amet faucibus. Ut eu justo elementum, ultrices elit in, eleifend enim.
</div>
</div>
<div id="stickyheader">
<a href="#disco">discography</a><span class="grey"> - </span>
<a href="#bio">biography</a><span class="grey"> - </span>
<a href="#press">press</a><span class="grey"> - </span>
<a href="#studio">studio</a><span class="grey"> - </span>
<a href="#contacts">contacts</a>
</div>
<div id="entry">
<div class="page">
<div class="separation"></div>
<div id="disco" class="ancre"></div>
<div class="bloc_bio">
<div class="legende">DISCOGRAPHY</div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas porttitor dignissim mollis. Nam tempor tristique ultricies. Nullam blandit aliquet augue, et elementum elit. Morbi at enim ut magna semper varius. Curabitur purus turpis, condimentum at ullamcorper vel, adipiscing vel enim. Phasellus hendrerit semper nisi quis molestie. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur egestas nibh nunc, ac facilisis dui ultricies eget. Integer vestibulum ultricies diam, ut laoreet risus. Morbi suscipit venenatis tortor sit amet faucibus. Ut eu justo elementum, ultrices elit in, eleifend enim.
</div>
<div class="separation"></div>
<div id="bio" class="ancre"></div>
<div class="bloc_bio">
<div class="legende">BIOGRAPHY</div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas porttitor dignissim mollis. Nam tempor tristique ultricies. Nullam blandit aliquet augue, et elementum elit. Morbi at enim ut magna semper varius. Curabitur purus turpis, condimentum at ullamcorper vel, adipiscing vel enim. Phasellus hendrerit semper nisi quis molestie. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur egestas nibh nunc, ac facilisis dui ultricies eget. Integer vestibulum ultricies diam, ut laoreet risus. Morbi suscipit venenatis tortor sit amet faucibus. Ut eu justo elementum, ultrices elit in, eleifend enim.
</div>
<div class="separation"></div>
<div id="press" class="ancre"></div>
<div class="bloc_bio">
<div class="legende">PRESS</div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas porttitor dignissim mollis. Nam tempor tristique ultricies. Nullam blandit aliquet augue, et elementum elit. Morbi at enim ut magna semper varius. Curabitur purus turpis, condimentum at ullamcorper vel, adipiscing vel enim. Phasellus hendrerit semper nisi quis molestie. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur egestas nibh nunc, ac facilisis dui ultricies eget. Integer vestibulum ultricies diam, ut laoreet risus. Morbi suscipit venenatis tortor sit amet faucibus. Ut eu justo elementum, ultrices elit in, eleifend enim.
</div>
<div class="separation"></div>
<div id="studio" class="ancre"></div>
<div class="bloc_bio">
<div class="legende">STUDIO</div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas porttitor dignissim mollis. Nam tempor tristique ultricies. Nullam blandit aliquet augue, et elementum elit. Morbi at enim ut magna semper varius. Curabitur purus turpis, condimentum at ullamcorper vel, adipiscing vel enim. Phasellus hendrerit semper nisi quis molestie. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur egestas nibh nunc, ac facilisis dui ultricies eget. Integer vestibulum ultricies diam, ut laoreet risus. Morbi suscipit venenatis tortor sit amet faucibus. Ut eu justo elementum, ultrices elit in, eleifend enim.
</div>
<div class="separation"></div>
</div>
</div>
</div>
和 javascript :
$(function(){
// Check the initial Poistion of the Sticky Header
var stickyHeaderTop = $('#stickyheader').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop ) {
$('#stickyheader').css({position: 'fixed', top: '0px'});
$('#stickyalias').css('display', 'block');
} else {
$('#stickyheader').css({position: 'static', top: '0px'});
$('#stickyalias').css('display', 'none');
}
});
});
//-------------------------------------------------- # SCROLL ----------------------------------------------------//
$(document).ready(function(){
$('a[href^="#"]').bind('click.smoothscroll',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top-90
//--OFFSET--//
}, 2000, 'swing', function () {
window.location.hash = target;
});
});
});
我的问题是我需要动态生成这个偏移量。每次单击我的菜单中的任何项目时,偏移量都需要检查我的菜单(#stickyheader)是否粘在我的页面顶部,然后计算我的菜单的高度(#stickyheader),这将是偏移量(+ 10px 用于填充底部)。如果我的菜单没有粘在顶部,所以显示我的菜单上的 div (#unstickyheader),偏移量应该是我的菜单的高度 (#stickyheader) + div 的高度 (#unstickyheader)。
我不知道如何计算两个 div 的总高度,具体取决于是否显示第一个 div,以及在我的菜单中的每次单击时计算...
你对那个怎么想的 ?还有其他解决方案吗?
我在这里做了一个 JSfiddle,http://jsfiddle.net/uFq2k/359/
示例中的偏移量是 90,因此当我在显示 div (#unstickyheader) 时单击菜单项时,我的菜单和我的部分标题之间的填充,例如“discography”是正确的,但是当我单击时之后的另一个链接,比方说传记,内边距太大……应该在 50 左右……
我真的希望你能理解我的问题,我很难解释,但如果你测试我的 JSfiddle,你应该明白这个问题。
非常感谢你的帮助 !