【发布时间】:2014-11-12 07:11:44
【问题描述】:
我正在按照本教程构建一个带有滚动功能的自定义粘性导航菜单。
我正在使用以下 jQuery 插件:
当点击链接时,滚动按预期工作;然而; .selected 类在单击且方向向下时未正确应用于菜单链接。
例如:
- 菜单项 1
- 菜单项 2
- 菜单项 3
当页面加载时,Menu item 1 用.selected 突出显示
类。
当我们点击Menu item 2 时,会发生滚动,但不会突出显示。
现在,如果我们点击 Menu item 3 会发生滚动并且突出显示 Menu item 2 而不是 Menu item 3
这是我正在使用的代码:
<nav class="section-navigation">
<ul>
<li><h5><a href="#item-1">item A</a></h5></li>
<li><h5><a href="#item-2">item B</a></h5></li>
<li><h5><a href="#item-3">item C</a></h5></li>
<li><h5><a href="#item-4">item D</a></h5></li>
</ul>
</nav>
<div class="section-content" id="item-1">some content for this section</div>
<div class="section-content" id="item-2">some content for this section</div>
<div class="section-content" id="item-3">some content for this section</div>
<div class="section-content" id="item-4">some content for this section</div>
jQuery('.section-navigation').waypoint('sticky', {
offset: 90 // Apply "stuck" when element 30px from top
});
jQuery(function() {
var sections = jQuery('.section-content');
var navigation_links = jQuery('nav a');
sections.waypoint({
handler: function(event, direction) {
var active_section;
active_section = jQuery(this);
if (direction === "up") active_section = active_section.prev();
var active_link = jQuery('nav a[href="#' + active_section.attr("id") + '"]');
navigation_links.closest('li').removeClass("selected");
active_link.closest('li').addClass("selected");
},
offset: '-20px'
});
jQuery('nav a[href^="#"]').on('click', function(event) {
event.preventDefault();
jQuery.scrollTo(
jQuery(this).attr("href"),
{
duration: 200,
//offset: { 'left':0, 'top':-0.15*jQuery(window).height() }
offset: { 'top':+0.15 }
}
);
});
});
我发现了另外三个相关的问题,但没有一个给出解决我问题的答案:
- Waypoints - Sticky Header Navigation. Links Are Highlighted Upon Scrolling Down But Not Scrolling Up
- jquery waypoints hilighting navigation
- jQuery Waypoints sticky nav when scrolling up via a click is 1px off
请帮我解决这个问题。谢谢。
编辑:这是小提琴:http://jsfiddle.net/uteqm28v/2/
【问题讨论】:
-
制作 JSFiddle -jsfiddle.net 我们可以帮助您快速
-
@Prog 我刚刚用小提琴更新了帖子。
标签: jquery navigation scrollto sticky jquery-waypoints