我的粘性标题行为出现问题。
期望的行为:
a)滚动到
.nav
的底部与section
的顶部相遇时,将active
类添加到选项卡,并保持其处于active
状态,直到.nav
到达下一个 section 的顶部。b)单击相应的 section 的
.tab
将始终导航到该 section 的顶部,并将active
类添加到选项卡。
因此,无论是通过滚动还是点击,选项卡的 active
状态始终保持,直到 .nav
跨入下一个 section
,在这种情况下,active
状态转移到该 section 的选项卡等等。
测试问题:
1) 在滚动到 Option Two
的一半时,该 .tab
的 active
状态丢失。
2) 使用 scrollTop
滚动到所选 section
的顶部,而不是 .container
的顶部。
class StickyNavigation {
constructor() {
this.currentId = null;
this.currentTab = null;
let self = this;
$(".tab").click(function() {
self.onTabClick(event, $(this));
});
$(".container").scroll(() => {
this.onScroll();
});
$(".container").resize(() => {
this.onResize();
});
}
/*Scrolls down to Tab selection*/
onTabClick(event, element) {
event.preventDefault();
let scrollTop = $(element.attr("href")).offset().top;
if (!$(".nav").hasClass("nav--top")) {
scrollTop = scrollTop;
}
$(".container").animate({
scrollTop: scrollTop
}, 600);
}
onScroll() {
this.navPosition();
this.tabAnimation();
}
navPosition() {
let offset = $(".sticky").offset().top + $(".sticky").height();
if ($(".container").scrollTop() > offset) {
$(".nav").addClass("nav--top");
} else {
$(".nav").removeClass("nav--top");
}
}
tabAnimation() {
$("section").each(function() {
var actual = $(this),
actualHeight = actual.height(),
actualAnchor = $(".sticky").find('a[href="#' + actual.attr("id") + '"]');
if (
actual.offset().top <= $(".container").scrollTop() &&
actual.offset().top + actualHeight > $(".container").scrollTop()
) {
actualAnchor.addClass("active");
} else {
actualAnchor.removeClass("active");
}
});
}
}
new StickyNavigation();
body {
position: fixed;
display: flex;
flex-direction: column;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow: hidden;
}
section {
height: 600px;
border: 2px solid white;
background: blue;
}
section:nth-child(2) {
background: red;
}
.container {
flex: 1;
display: flex;
position: relative;
flex-direction: column;
overflow: auto;
}
.long {
height: 1200px;
}
.header {
height: 75px;
background: green;
}
.hero {
background: silver;
flex: 0;
border: 1px solid;
}
.nav {
background: white;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.nav--top {
position: fixed;
top: 75px;
}
.sticky {
background: white;
display: flex;
flex-direction: row;
flex-wrap: wrap;
position: relative;
}
.tab {
padding: 30px 45px;
position: relative;
}
.tab.active {
background: #6567c5;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="header">
<h1>Header</h1>
</div>
<div class="container">
<div class="hero">
<h1>Hero</h1>
</div>
<div class="sticky">
<nav role="navigation" class="nav">
<a class="tab" href="#One">Option One</a>
<a class="tab" href="#Two">Option Two</a>
</nav>
</div>
<div class="main">
<section id="One">
</section>
<section class="long" id="Two">
</section>
</div>
</div>
</div>