How to merge two javascript codes into one? - javascript

Both scripts work separately but I'm trying to merge them into one. All my efforts didn't work.
It's a simple script to hide the navbar on scroll-down, and show it up on scroll-up with a white background. But when the page is scrolled all the way up, I need the navbar background to become transparent.
<script>
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 = "-85px";
}
prevScrollpos = currentScrollPos;
}
</script>
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 85 || document.documentElement.scrollTop > 85) {
document.getElementById("navbar").style.background = "#fff";
} else {
document.getElementById("navbar").style.background = "none";
}
}
</script>

There are two ways to solve your problem:
Solution 1: Use window.addEventListener to avoid overriding window.onscroll property
This solution requires the least amount of effort, as it is simply switching out the method of listening to the scroll event. It is also helpful if both logic reside in separate files and you don't want to combine them.
<script>
let prevScrollpos = window.pageYOffset;
window.addEventListener('scroll', () => {
const currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-85px";
}
prevScrollpos = currentScrollPos;
});
</script>
<script>
window.addEventListener('scroll', scrollFunction);
function scrollFunction() {
if (document.body.scrollTop > 85 || document.documentElement.scrollTop > 85) {
document.getElementById("navbar").style.background = "#fff";
} else {
document.getElementById("navbar").style.background = "none";
}
}
</script>
Solution 2: Combine logic from both functions into one
This method moves all your scroll-related logic into a single event listener for the ease of maintenance. In this case, you can still use window.onscroll to assign the function, even though I would strongly encourage not to do so:
<script>
window.addEventListener('scroll', () => {
navBarScrollFunction();
scrollFunction();
});
let prevScrollpos = window.pageYOffset;
function navBarScrollFunction() {
const currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-85px";
}
prevScrollpos = currentScrollPos;
}
function scrollFunction() {
if (document.body.scrollTop > 85 || document.documentElement.scrollTop > 85) {
document.getElementById("navbar").style.background = "#fff";
} else {
document.getElementById("navbar").style.background = "none";
}
}
</script>

Related

Combining two similiar .js functions to one

I know this is basic for most of you but im not that experienced on this language. So when i add both of the codes to functions.php they dont work. But it only works when i use only one of them. So im thinking maybe it would work if they both were in the same code lines. I tried to do that but couldnt make it work.
This is the first function:
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 90 ||
document.documentElement.scrollTop > 90)
{
document.getElementById("quadmenu_0")
.style.padding = "20px 0px";
}
else {
document.getElementById("quadmenu_0")
.style.padding = "180px 0px 40px";
}
}
And this is the second function:
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 150 ||
document.documentElement.scrollTop > 150)
{
document.getElementById("ast-mobile-header")
.style.backgroundColor = "red";
}
else {
document.getElementById("ast-mobile-header")
.style.backgroundColor = "white";
}
}
You can call both functions within the onScroll listener. They will need to of course have different names (and best to choose something more logical than the below example)
window.onscroll = function() {
scrollFunction1()
scrollFunction2()
};
function scrollFunction1() {
if (document.body.scrollTop > 90 ||
document.documentElement.scrollTop > 90) {
document.getElementById("quadmenu_0")
.style.padding = "20px 0px";
} else {
document.getElementById("quadmenu_0")
.style.padding = "180px 0px 40px";
}
}
function scrollFunction2() {
if (document.body.scrollTop > 150 ||
document.documentElement.scrollTop > 150) {
document.getElementById("ast-mobile-header")
.style.backgroundColor = "red";
} else {
document.getElementById("ast-mobile-header")
.style.backgroundColor = "white";
}
}

Three cases on scroll - function inside an if

I need to set three different background on a navbar:
1. No background if the page is less than 400 px of scrolling
2. Two different colors if the scroll of the page is more than 400 px:
a) blue when I scroll down
b) green when I scroll up.
I've tried to use the following code, but it seems like after I enter in the first IF, the function continue to work even if the page is less than 400px.
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 400 || document.documentElement.scrollTop > 400) {
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("nav1").style.background = "rgba(0, 41, 51,1)";
} else {
lastScroll = currentScroll;
document.getElementById("nav1").style.background = "rgba(68,78,36,1)";
}
prevScrollpos = currentScrollPos;
} else {
document.getElementById("nav1").style.background = "rgba(0,0,0,0)";
}
}
Thanks!
Do not attempt to assign two functions to window.onscroll, which is a property and can hold only one function.
Here is what is going on with your current code:
An annonymous function is declared (it calls scrollFunction) and assigned to window.onscroll
At the very first scroll, scrollFunction is called. If the page has not scrolled yet beyond 400px, the if block is not executed.
As soon as the page goes beyond 400px, prevScrollpos is declared... Then the function previously assigned to window.onscroll is overwriten with a new one.
That is why the comparison for 400px isn't done after that. It is out of that second function. The first one got lost in the nothingness.
Here is what you want to achieve:
// This variable needs to be global
let prevScrollpos = 0;
// This getElement can also be global
let nav1 = document.getElementById("nav1")
function scrollFunction() {
// This varable needs to be local
let currentScrollPos = window.pageYOffset;
if (document.body.scrollTop > 400 || document.documentElement.scrollTop > 400) {
// Determine scroll direction
if (prevScrollpos > currentScrollPos) {
nav1.style.background = "rgba(0, 41, 51,1)";
} else {
nav1.style.background = "rgba(68,78,36,1)";
}
}
// If below 400px
else {
nav1.style.background = "rgba(0,0,0,0)";
}
// Update this variable for the next iteration
prevScrollpos = currentScrollPos;
// For this demo only
console.clear()
console.log(currentScrollPos)
}
// Assign the scrollFunction reference to the window property
window.onscroll = scrollFunction;
body {
height: 1000px;
}
#nav1{
position: sticky;
top: 4px;
height: 100px;
border: 1px solid black;
}
<div id="nav1"></div>
You can use this script:
<script>
window.onscroll = function () { myFunction() };
function myFunction() {
if (document.body.scrollTop > 400 || document.documentElement.scrollTop > 400) {
var lastScrollTop = 0;
window.addEventListener("scroll", function () {
var st = window.pageYOffset || document.documentElement.scrollTop;
if (st > lastScrollTop) {
document.getElementById("nav").style.background = "rgba(0, 41, 51,1)";
} else {
document.getElementById("nav").style.background = "rgba(68,78,36,1)";
}
lastScrollTop = st <= 0 ? 0 : st;
}, false);
} else {
document.getElementById("nav").style.background = "rgba(0,0,0,0)";
}
}
</script>

Javascript in Footer Not Loading in Wordpress

I have the following code for hiding and showing a navigation bar on my Wordpress website:
$(document).scroll(function() {
var y = $(this).scrollTop();
if (y > 800) {
$("#pn-navigation-bar").fadeIn();
} else {
$("#pn-navigation-bar").fadeOut();
}
});
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos < currentScrollPos) {
document.getElementById("pn-navigation-bar").style.opacity = 0;
document.getElementsByClassName("readingProgressbar")[0].style.display = "block";
} else if (prevScrollpos > currentScrollPos) {
document.getElementById("pn-navigation-bar").style.opacity = 1;
document.getElementsByClassName("readingProgressbar")[0].style.display = "none";
}
prevScrollpos = currentScrollPos;
};
And the following CSS to prevent it from loading the first time the page loads:
#pn-navigation-bar {
position: fixed;
top: 0;
width: 100%;
transition: all .3s ease-in-out;
display: none;
}
Ideally, this should be hiding the Navigation bar shown below while the user scrolls down and show it when a scroll up is detected.
However, as you can see if you visit the website here, this does not happen. This was working fine till a few days ago, but I don't know what has gone wrong. I've been optimizing my website for speed (JS/CSS minification) but I doubt that this gets affected. What can I do to fix this?
As suggested by users #mplungjan and #Howard E, Wordpress runs jQuery.noConflict. To prevent conflict with other JS librares, Wordpress will not recognize $ to be JQuery code and usually expects Jquery in place of the traditional $
To fix this, the easiest way is to map $ to JQuery.
According to this article, The code for this is:
(function($) {
//Insert Code Here
})( jQuery );
To correct the error, the code becomes:
<script>
(function($) {
$(document).scroll(function() {
var y = $(this).scrollTop();
if (y > 800) {
$("#pn-navigation-bar").fadeIn();
} else {
$("#pn-navigation-bar").fadeOut();
}
});
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos < currentScrollPos) {
document.getElementById("pn-navigation-bar").style.opacity = 0;
document.getElementsByClassName("readingProgressbar")[0].style.display = "block";
} else if (prevScrollpos > currentScrollPos) {
document.getElementById("pn-navigation-bar").style.opacity = 1;
document.getElementsByClassName("readingProgressbar")[0].style.display = "none";
}
prevScrollpos = currentScrollPos;
};
})( jQuery );
</script>

Why Is This "OnScroll" JavaScript Function Not Being Called?

I have a JavaScript code that, when the page is being scrolled DOWN, the navigation bar should hide (or margin-top: -100px). But, when the page is being scrolled up, the navigation bar should appear (or margin-top: 0).
I don't understand why this function is not being called when the page is scrolled. Is there something wrong in my code? Please look below:
let prevScrollpos = window.pageYOffset;
window.onscroll = function() {
let currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.marginTop = "0";
} else {
document.getElementById("navbar").style.marginTop = "-100px";
}
prevScrollpos = currentScrollPos;
}
The main reason that I know that the function isn't being called, is that when I added some simple script to the function (such as alert("Hello World!"), that did not happen either.
Thank you, and any help is appreciated.
Do you have multiple window.onscroll declarations in your codebase? If so you're overwriting them each time.
let prevScrollpos = window.pageYOffset;
window.addEventListener('scroll', function() {
let currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.marginTop = "0";
} else {
document.getElementById("navbar").style.marginTop = "-100px";
}
prevScrollpos = currentScrollPos;
});

Navbar becomes sticky when scrolling up even 1px - I want it to appear only when scrolling more than 10px

I'm working on a navbar that is always sticky on mobile and disappears when scrolling down but appears again when scrolling up on desktop.
Now it works perfectly well but I have one problem. On desktop, it becomes sticky when scrolling up right away. Even if I scroll just a little bit which can be annoying.
I want it to appear only when scrolling more than let's say 10px. So I'd need to add some threshold. I guess it could be done with JS but I'm a beginner.
Here's a fiddle with what I have now: https://jsfiddle.net/zsoltszilvai/zfqd901m/4/
I'd appreciate any help :)
var sticky = header.offsetTop;
var prevScrollpos = window.pageYOffset;
function myFunction(x) {
if (x.matches) {
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
}
} else {
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
}
} else {
document.getElementById("navbar").style.top = "-58px";
if (window.pageYOffset < sticky) {
header.classList.remove("sticky");
}
}
prevScrollpos = currentScrollPos;
}
}
}
You need to change prevScrollpos > currentScrollPos to prevScrollpos - currentScrollPos > 10, only tricky part is to not update prevScrollpos if the difference is smaller than the threshold.
var prevScrollpos = window.pageYOffset;
var SCROLL_UP_THRESHOLD = 200
function myFunction(x) {
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
console.log(prevScrollpos, currentScrollPos)
if (prevScrollpos > currentScrollPos) {
if (prevScrollpos - currentScrollPos < SCROLL_UP_THRESHOLD)
return console.log("not opening")
document.getElementById("navbar").style.top = "0";
if (window.pageYOffset > sticky) {
header.classList.add("sticky");
}
prevScrollpos = currentScrollPos;
} else {
document.getElementById("navbar").style.top = "-58px";
if (window.pageYOffset < sticky) {
header.classList.remove("sticky");
}
prevScrollpos = currentScrollPos;
}
}
}

Categories