Skip to content

Commit 42f571f

Browse files
committed
improved navigation-scoll method
1 parent 9e4fca6 commit 42f571f

File tree

1 file changed

+47
-49
lines changed
  • lib/modules/navigations-widgets/public/js

1 file changed

+47
-49
lines changed

lib/modules/navigations-widgets/public/js/lean.js

+47-49
Original file line numberDiff line numberDiff line change
@@ -59,71 +59,69 @@ apos.utils.widgetPlayers['navigations'] = function(el, data, options) {
5959
}
6060
}
6161

62-
// Remove the ugly focus ring on google chrome but keep it if user really uses keyboard
63-
function handleFirstTab(e) {
64-
if (e.keyCode === 9) { // the "I am a keyboard user" key
65-
document.body.classList.add('user-is-tabbing');
66-
window.removeEventListener('keydown', handleFirstTab);
62+
// Smooth scroll to anchor
63+
function scrollToAnchor(anchor) {
64+
var target = anchor.hash;
65+
var targetElement = document.getElementById(target.substring(1));
66+
var targetOffset = targetElement.getBoundingClientRect().top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
67+
if (window.scrollTo) {
68+
window.scrollTo({
69+
top: targetOffset,
70+
behavior: 'smooth'
71+
});
72+
} else {
73+
window.scroll(0, targetOffset);
74+
}
75+
if (history.pushState) {
76+
history.pushState(null, null, target);
77+
} else {
78+
location.hash = target;
79+
}
80+
}
81+
82+
// Set active link for scroll position
83+
function setActiveLinkForScrollPosition() {
84+
var scrollPos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
85+
var anchorScrollLinks = document.querySelectorAll('.anchor-scroll');
86+
var activeLinkFound = false;
87+
for (var i = 0; i < anchorScrollLinks.length; i++) {
88+
var link = anchorScrollLinks[i];
89+
var target = link.hash;
90+
var refElement = document.getElementById(target.substring(1));
91+
if (refElement.offsetTop <= scrollPos + 200 && refElement.offsetTop + refElement.offsetHeight > scrollPos + 200) {
92+
if (!activeLinkFound) {
93+
link.parentNode.classList.add('active');
94+
activeLinkFound = true;
95+
} else {
96+
link.parentNode.classList.remove('active');
97+
}
98+
} else {
99+
link.parentNode.classList.remove('active');
100+
}
67101
}
68102
}
69-
window.addEventListener('keydown', handleFirstTab);
70103

71-
// Anchor jump with scroll anchors
104+
// Add click handler to anchor links
72105
var anchorJumpLinks = document.querySelectorAll('.anchor-jump[href^="#"]');
73106
for (var i = 0; i < anchorJumpLinks.length; i++) {
74107
var anchor = anchorJumpLinks[i];
75-
addEvent(anchor, 'click', function(e) {
108+
anchor.addEventListener('click', function(e) {
76109
e.preventDefault();
77-
window.removeEventListener("scroll", scrollHandler);
78-
var target = this.hash;
79-
var targetElement = document.getElementById(target.substring(1));
80-
var targetOffset = targetElement.getBoundingClientRect().top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0);
81-
if (window.scrollTo) {
82-
window.scrollTo({
83-
top: targetOffset,
84-
behavior: 'smooth'
85-
});
86-
} else {
87-
window.scroll(0, targetOffset);
88-
}
89-
if (history.pushState) {
90-
history.pushState(null, null, target);
91-
} else {
92-
location.hash = target;
93-
}
110+
scrollToAnchor(this);
94111
});
95112
}
96113

97-
// Set anchors when scroll
114+
// Add scroll handler to update active link
98115
var scrolling = false;
99-
function scrollHandler() {
116+
window.addEventListener('scroll', function() {
100117
scrolling = true;
101-
}
102-
addEvent(window, 'scroll', scrollHandler);
118+
});
103119

104-
// Throtteling function to prevent browser flooding
120+
// Throttling function to prevent browser flooding
105121
setInterval(function() {
106122
if (scrolling) {
107123
scrolling = false;
108-
var scrollPos = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
109-
var anchorScrollLinks = document.querySelectorAll('.anchor-scroll');
110-
for (var i = 0; i < anchorScrollLinks.length; i++) {
111-
var link = anchorScrollLinks[i];
112-
var target = link.hash;
113-
var refElement = document.getElementById(target.substring(1));
114-
if (refElement.offsetTop <= scrollPos + 200 && refElement.offsetTop + refElement.offsetHeight > scrollPos + 200) {
115-
link.parentNode.className += " active";
116-
if (window.location.pathname !== target) {
117-
if (history.pushState) {
118-
history.pushState(null, null, target);
119-
} else {
120-
location.hash = target;
121-
}
122-
}
123-
} else {
124-
link.parentNode.className = link.parentNode.className.replace(/\bactive\b/, "");
125-
}
126-
}
124+
setActiveLinkForScrollPosition();
127125
}
128126
}, 500);
129127

0 commit comments

Comments
 (0)