<div class="news-navbar" data-behaviour="news-navbar">
<div class="news-navbar__wrapper">
<nav class="news-navbar__nav-list" data-behavior="news-navbar--nav-list">
<a href="#club-section" data-nav-to="club-section" class="club-section">
Club Section
</a>
<a href="#casual-section" data-nav-to="casual-section" class="casual-section">
Casual Section
</a>
</nav>
</div>
</div>
No notes defined.
{
"data": [
{
"title": "Club Section",
"id": "club-section",
"additionalClasses": ""
},
{
"title": "Casual Section",
"id": "casual-section",
"additionalClasses": ""
}
]
}
export default parentElement => {
const sections = document.querySelectorAll('[data-content="news-content"]');
const navLinks = parentElement.querySelectorAll('a');
const stickyEl = parentElement.querySelector('.news-navbar__wrapper');
const stickyPosition = stickyEl.getBoundingClientRect().top;
const offset = -10;
const MOBILE_BREAKPOINT = 500;
if (window.innerWidth > MOBILE_BREAKPOINT) {
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
const currentScrollPos = window.pageYOffset;
if (currentScrollPos >= sectionTop - sectionHeight / 3) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.classList.contains(current)) {
link.classList.add('active');
}
link.addEventListener('click', event => {
event.preventDefault();
const targetId = event.target.getAttribute('data-nav-to');
const tagetElm = document.querySelector(`#${targetId}`).offsetTop;
let initial = 0;
if (parentElement.querySelector('a.active')) {
const currentElm = parentElement
.querySelector('a.active')
.getAttribute('data-nav-to');
initial = document.querySelector(`#${currentElm}`).offsetTop;
}
if (initial < tagetElm) {
window.scrollTo(0, tagetElm);
} else {
const sctollTo =
tagetElm -
document.querySelector('.global-fixed-header').clientHeight;
window.scrollTo(0, sctollTo);
}
});
});
if (window.pageYOffset >= stickyPosition + offset) {
const headerMargin = document.querySelector('.global-fixed-header')
.style.top;
if (headerMargin.replace('px', '') === '0') {
stickyEl.style.top = `
${document.querySelector('.global-fixed-header').clientHeight}px`;
} else {
stickyEl.style.top = '';
}
parentElement.classList.add('nav-sticky');
} else {
parentElement.classList.remove('nav-sticky');
}
});
}
};
html {
scroll-behavior: smooth;
}
.news-navbar {
display: flex;
justify-content: center;
width: 100%;
left: 0;
z-index: 9;
min-height: 4.2rem;
&__nav-list {
display: none;
}
@media screen and (min-width: $mq-small) {
&__nav-list {
display: flex;
width: 100%;
max-width: 102rem;
margin: 0 auto;
a {
@include text-l;
font-weight: 400;
letter-spacing: -0.01em;
padding-bottom: 0.4rem;
padding-top: 0.4rem;
text-decoration: none;
text-transform: uppercase;
color: $color-interface-light;
border-bottom: 2px solid $light-blue;
width: 100%;
text-align: center;
&:not(:last-child) {
padding-right: 1rem;
}
&.active {
color: #011e41;
border-color: #e1261c;
}
&:hover {
color: $blue;
border-bottom: 2px solid $blue;
}
}
}
&__wrapper {
width: 100%;
transition: position 0.4s ease-in-out 0s, top 0.4s ease-in-out 0s;
}
&.nav-sticky {
.news-navbar__wrapper {
position: fixed;
background: $white;
z-index: 10;
top: 0;
}
}
}
}
@media screen and (min-width: $mq-medium) {
.news-navbar {
padding: 0 6rem;
}
}
[data-brand=''],
[data-brand='englandfootball'],
[data-brand='englandfootball--supplimentary'] {
.news-navbar {
@media screen and (min-width: $mq-small) {
&__nav-list {
a {
font-family: $text-font2;
line-height: 2.2rem;
font-weight: 700;
letter-spacing: 0.02em;
padding-bottom: 2.2rem;
padding-top: 2.2rem;
}
}
}
}
}
<div class="news-navbar" data-behaviour="news-navbar">
<div class="news-navbar__wrapper">
<nav class="news-navbar__nav-list" data-behavior="news-navbar--nav-list">
{{#each data}}
<a href="#{{id}}" data-nav-to="{{id}}" class="{{id}}">
{{title}}
</a>
{{/each}}
</nav>
</div>
</div>