window.addEventListener('scroll', function() {
var topHeader = document.querySelector('.topheader');
var isScrolled = window.scrollTop > 1;
var otherCondition = true;
var shouldToggle = isScrolled && otherCondition;
topHeader.classList.toggle("active", shouldToggle);
});
When it comes to upscrolling, that can only be determined by comparing with a previous scroll amount.
The other situation of being within 100px is much easier to check.
I’ll leave you to work on this yourself for a bit, as that’s where the most learning occurs.
How should we register the previous scroll? Because the scroll(when the user is interacting with the scrolling event) is a continuous and often random(scrolling up and down) process. Current can be done, but previous?
If I put var lastScroll = 0; outside window addeventlistener I get NAN, and if I put it just inside addvenetlistener, irrespective of what is assigned in the bottom when we had set →
Yes you need to research the difference between that and scrollTop and why you might need the version I posted above.
You may be interested in this old version that @Paul_Wilkins tidied up for me in an old thread. It uses jquery but is the same idea. It also has a debounce function added so that the scroll event isn’t slowing down the page.
The scroll event fires continuously when scrolling so you really need to debounce it or it slows heavy pages down.
I was doing something else with Javascript and came to know about this property: getBoundingClientRect() → Mozilla Link Here
I used this property to get the anticipated result, and come up with this working version:
var lastScroll = 0;
window.addEventListener('scroll', function() {
var stickyHeader = document.querySelector(".header");
var isScrolled = window.pageYOffset > 1;
currentScroll = document.body.getBoundingClientRect().top;
var scrollDirection = currentScroll > lastScroll;
var shouldToggle = isScrolled && scrollDirection;
stickyHeader.classList.toggle("active", shouldToggle);
lastScroll = currentScroll;
});
It has not minimized the lines of code but utilizes another property to achieve the desired result.
Top is treated as 0 point so this will give negative value: document.body.getBoundingClientRect().top so we have to adjust equality/inequality in scrollDirection reciprocal to what we did in previous version.
In web pages we have one click page scrolls, I have designed that but I wanted to achieve that scroll should be smooth. I am using vanilla JS code. I know how to do that in JS, but in JS?
var lastScroll = 0;
window.addEventListener('scroll', function() {
var stickyHeader = document.querySelector(".header");
var isScrolled = window.pageYOffset > 1;
var currentScroll = document.body.getBoundingClientRect().top;
var scrollDirection = currentScroll > lastScroll;
var shouldToggle = isScrolled && scrollDirection;
stickyHeader.classList.toggle("active", shouldToggle);
lastScroll = currentScroll;
var idArrowScrollUp = document.querySelector("#arrowScrollUp");
var arrowScrollUp = window.pageYOffset >= 50;
idArrowScrollUp.classList.toggle("arrowScrollUp", arrowScrollUp);
});
This part is handling that one click whole page scroll to top →
var idArrowScrollUp = document.querySelector("#arrowScrollUp");
var arrowScrollUp = window.pageYOffset >= 50;
idArrowScrollUp.classList.toggle("arrowScrollUp", arrowScrollUp);
That’s right sorry for the typo mistake. I have uploaded the code online. In the bottom right you will find an arrow, which on click will send the whole page up that scroll is unsmooth. There must be some time function and may be css can fit here.