I have a page with a lot of text that requires some amount of scrolling. I was able to get a button, when clicked, to shoot to the top of the page I am on. But when at the top of the page, I am wanting this button to switch to another link that goes to the homepage.
Bonus Points: How would I change the text to also switch from "top" to "home"? I have not tackled this hurdle because I figured my issue with switching the href would correlate to this obstacle.
JS:
window.onscroll = function() { scrollFunction() };
function scrollFunction() {
document.getElementById("scroll-to-top-button").classList.toggle("show");
window.onclick = function(event) {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
window.location = "#top";
} else if(document.body.scrollTop = 0){
window.location.href = "https://homepage.html";
}
}
}
html:
<a href="" class="scroll-to-top-button active" onclick="scrollFunction()" data-smooth-scroll=""><div class="fas fa-angle-up">top</div></a>
I have tried using window.scrollY instead of .scrollTop - but I have not touched scrolling elements prior to this. I am a little fuzzy with how to indicate if I have scrolled vs not scrolled. I do not know if my issue is because my if, else elements are not correct - or if it is something else?
window.onclick = ...out of the scroll listener. Or actually, you've to pull everything out of the scroll handler.