gpt4 book ai didi

javascript - anchor 导航与粘在顶部菜单...需要添加动态偏移到 anchor

转载 作者:行者123 更新时间:2023-11-28 08:46:37 27 4
gpt4 key购买 nike

我的菜单和基于#anchors 的导航有问题。我正在使用 JS 函数在滚动时将菜单置于顶部(#stickyheader)。我的菜单上有一个 div,滚动时会消失(#unstickyheader)。该 div 的高度不是固定的,它将取决于其内容长度以及屏幕的宽度。我的菜单垂直链接到页面的不同部分,在处理 anchor 导航的 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 你应该理解这个问题。

非常感谢您的帮助!

最佳答案

我相信这就是您想要的:http://jsfiddle.net/B5dYv/

问题是#stickyheader 在固定显示值和其他显示值之间切换。这导致其他元素偏移量发生变化,因为固定有效地从 dom 堆栈中删除了该元素,最终导致了错误的滚动值。

我所做的不是在静态和固定之间切换#stickyheader,它现在是相对的和固定的。

此外,滚动问题的主要解决方法是通过检查 #stickyheader 的位置和高度来有条件地设置滚动偏移,从而获得您想要的动态偏移。

$(document).ready(function () {
$('a[href^="#"]').bind('click.smoothscroll', function (e) {
e.preventDefault();

var target = this.hash,
$target = $(target);

var offset;
if($('#stickyheader').css('position') == 'relative'){
offset = $('#stickyheader').outerHeight(true)*2;
}else{
offset = $('#stickyheader').outerHeight(true);
}
$('html, body').stop().animate({
'scrollTop': $target.offset().top - offset
//--OFFSET--//
}, 2000, 'swing', function () {
window.location.hash = target;
});
});
});

关于javascript - anchor 导航与粘在顶部菜单...需要添加动态偏移到 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19688787/

27 4 0