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>
Related
My company wanted our website header to disappear on scroll down but reappear on scroll up, which I was able to do with JS and CSS - it works fine on Desktop but doesn't work on mobile. I am using Wordpress and Beaver Builder (I have escalated to Beaver Builder in case something might be blocking it?)
JAVASCRIPT:
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("header-global").style.top = "0";
} else {
document.getElementById("header-global").style.top = "-100px";
}
prevScrollpos = currentScrollPos;
}
And the following CSS:
#header-global {
position:fixed;
Width: 100%;
transition: 0.3s;
z-index: 20;
}
The function works perfectly fine on desktop but I not on mobile. I have the Beaver Builder header plugin, which I have confirmed sticky header is not activated. Is there anything else blocking it for mobile / am I doing something wrong? Note that my JS knowledge is limited..
Your help would be appreciated.
Needed to decrease the px as shown below
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("header-global").style.top = "0";
} else {
document.getElementById("header-global").style.top = "-200px";
}
prevScrollpos = currentScrollPos;
}
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>
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>
First, let me clarify that I am extremely new to this, so I apologize for using the wrong terminology.
I'm looking to recreate Morning Brew's sticky footer opt-in form (click here for reference), but the code I'm currently using only tracks pixels. Is there a way I can make it, so it looks for div tags instead?
<script>
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos < 4200 && prevScrollpos > 200) {
document.getElementsByClassName("_form")[0].style.display = "block";
} else {
document.getElementsByClassName("_form")[0].style.display = "none";
}
prevScrollpos = currentScrollPos;
}
</script>
You can use window and get scroll event by this way:
let element = document.getElementById('footer');
window.addEventListener('scroll', function(e){
let Y = window.scrollY;
console.log(Y);
Y > 600 ? element.style.display = 'block' : element.style.display = 'none';
})
body{
height: 1600px;
}
<div id="footer">Element to fix</div>
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;
});