Good morning,
I need a help, I have two functions that use the onscroll event, a function to appear a "Back to Top" button where when it is presented to the user it can be triggered and will be returned to the top of the page and another of hiding part of the navbar of the page when descending a little the page, however, when including both, only one of them works. My question is this, can I just include an onscroll event in the file? Is there any way I can apply both functions to the same file?
Below are both the codes in js that I'm using:
Hide NavBar:
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
};
Back to Top button:
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop >
20) {
document.getElementById("Topo").style.display = "block";
} else {
document.getElementById("Topo").style.display = "none";
}
}
function VoltarTopo() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
};