Advertisement
Udoro

Scroll to reveal/hide header - JS

Feb 25th, 2022
1,919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener ('DOMContentLoaded', ()=> {
  2.  
  3.  
  4.     const   body = document.body,
  5.             scrollUp = "scroll-up",
  6.             scrollDown = "scroll-down";
  7.     let     lastScroll = 0;
  8.  
  9.  
  10.     //check for scrol action
  11.  
  12.     window.addEventListener ('scroll', ()=> {
  13.        
  14.        
  15.    
  16.        
  17.         //hide and reveal header
  18.        
  19.             const currentScroll = window.pageYOffset;
  20.               if (currentScroll <= 200) {
  21.                 body.classList.remove(scrollUp);
  22.                 return;
  23.               }
  24.  
  25.               if (currentScroll > lastScroll && !body.classList.contains(scrollDown)) {
  26.                 // down
  27.                 body.classList.remove(scrollUp);
  28.                 body.classList.add(scrollDown);
  29.  
  30.               } else if (
  31.                 currentScroll < lastScroll &&
  32.                 body.classList.contains(scrollDown)
  33.               ) {
  34.                 // up
  35.                 body.classList.remove(scrollDown);
  36.                 body.classList.add(scrollUp);
  37.  
  38.               }
  39.               lastScroll = currentScroll;
  40.            
  41.  
  42.        
  43.        
  44.     })
  45.     // end scroll action
  46.  
  47.  
  48.  
  49. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement