Skip to content

Commit

Permalink
[BUGFIX] Correct scroll position and prevent click if target exists o…
Browse files Browse the repository at this point in the history
…n site

Resolves: #1259
  • Loading branch information
benjaminkott committed Jun 20, 2023
1 parent 6c63f70 commit f589ba0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions Resources/Public/JavaScript/Src/bootstrap.smoothscroll.js
@@ -1,28 +1,26 @@
window.addEventListener('DOMContentLoaded', function () {

/*
* Smooth Sroll
* Smooth Scroll
*/
const ankers = document.querySelectorAll('a[href*="#"]:not([href^="http"]):not([href$="#"])');
const ankers = document.querySelectorAll('a[href*="#"]:not([href$="#"])');
ankers.forEach(function (anker) {
anker.addEventListener('click', function (event) {
event.preventDefault();
const element = event.currentTarget;
if (location.pathname.replace(/^\//, '') === element.pathname.replace(/^\//, '')
&& location.hostname === element.hostname
&& element.dataset.toggle === undefined
&& element.dataset.slide === undefined) {
&& location.hostname === element.hostname) {
let target = document.querySelectorAll(element.hash.replace(/(:|\.|\[|\]|,|=|\/)/g, '\\$1'));
target = target.length && target || document.querySelectorAll('[name=' + element.hash.slice(1) + ']');
if (target.length) {
event.preventDefault();
let targetOffset = target[0].getBoundingClientRect().top + window.scrollY;
const navbar = document.querySelector('.navbar-fixed-top');
if (navbar && targetOffset !== 0) {
targetOffset -= navbar.getBoundingClientRect().height;
}
scroll({ top: targetOffset, behavior: "smooth" });
target[0].setAttribute('tabindex', '-1');
target[0].focus();
scroll({ top: targetOffset, behavior: "smooth" });
return false;
}
}
Expand Down

0 comments on commit f589ba0

Please sign in to comment.