Fix issues caused by missing gttBtn when enableGoToTop is false

This commit is contained in:
Yoni 2021-08-21 14:00:10 +03:00
parent 01f9e43e59
commit 9c63e8cb3e
1 changed files with 41 additions and 33 deletions

View File

@ -243,33 +243,35 @@
// ========================= go to top ========================= // ========================= go to top =========================
var gttBtn = document.getElementById("gtt"); {{ if $.Param "enableGoToTop" }}
gttBtn.style.display = "none"; var gttBtn = document.getElementById("gtt");
gttBtn.addEventListener('click', function () { gttBtn.style.display = "none";
if (window.document.documentMode) { gttBtn.addEventListener('click', function () {
document.documentElement.scrollTop = 0; if (window.document.documentMode) {
} else { document.documentElement.scrollTop = 0;
scrollToTop(250); } else {
} scrollToTop(250);
});
function scrollToTop(scrollDuration) {
var scrollStep = -window.scrollY / (scrollDuration / 15);
var scrollInterval = setInterval(function () {
if (window.scrollY != 0) {
window.scrollBy(0, scrollStep);
} }
else clearInterval(scrollInterval); });
}, 15);
}
var scrollFunction = function () { function scrollToTop(scrollDuration) {
if (document.body.scrollTop > 250 || document.documentElement.scrollTop > 250) { var scrollStep = -window.scrollY / (scrollDuration / 15);
gttBtn.style.display = "block"; var scrollInterval = setInterval(function () {
} else { if (window.scrollY != 0) {
gttBtn.style.display = "none"; window.scrollBy(0, scrollStep);
}
else clearInterval(scrollInterval);
}, 15);
} }
}
var scrollFunction = function () {
if (document.body.scrollTop > 250 || document.documentElement.scrollTop > 250) {
gttBtn.style.display = "block";
} else {
gttBtn.style.display = "none";
}
}
{{ end }}
// ============================================================ // ============================================================
@ -347,15 +349,19 @@
window.onscroll = function () { window.onscroll = function () {
scrollFunction(); {{ if $.Param "enableGoToTop" }}
scrollFunction();
{{ end }}
var st = window.pageYOffset || document.documentElement.scrollTop; var st = window.pageYOffset || document.documentElement.scrollTop;
if (st > lastScrollTop) { // scroll down if (st > lastScrollTop) { // scroll down
if (st < 250) { {{ if $.Param "enableGoToTop" }}
gttBtn.style.display = "none"; if (st < 250) {
} else { gttBtn.style.display = "none";
gttBtn.style.display = "block"; } else {
} gttBtn.style.display = "block";
}
{{ end }}
if (st < 45) { if (st < 45) {
return null; return null;
@ -429,9 +435,11 @@
} }
} }
} else { // scroll up } else { // scroll up
if (st < 250) { {{ if $.Param "enableGoToTop" }}
gttBtn.style.display = "none"; if (st < 250) {
} gttBtn.style.display = "none";
}
{{ end }}
if (navbar.classList.contains('scrolling')) { if (navbar.classList.contains('scrolling')) {
if (navbar.classList.contains('navbar--hide')) { if (navbar.classList.contains('navbar--hide')) {