简体   繁体   中英

How can I create a hide on scroll down and show on scroll up menu whitout jquery or other dependencies

I am working on a website that needs to be fast and light. I used this script before on other websites but with jquery library. For this specific website I need to run very fast since most of the users have low mobile internet speed. For this reason I want just plain javascript to do this task.

I'm not very familiar with javascript so I need some help that's why I'm asking for.

Can anyone help me whit this please?

Thank you, John

 // Hide Header on on scroll down var didScroll; var lastScrollTop = 0; var delta = 5; var navbarHeight = $('header').outerHeight(); $(window).scroll(function(event){ didScroll = true; }); setInterval(function() { if (didScroll) { hasScrolled(); didScroll = false; } }, 250); function hasScrolled() { var st = $(this).scrollTop(); // Make sure they scroll more than delta if(Math.abs(lastScrollTop - st) <= delta) return; // If they scrolled down and are past the navbar, add class .nav-up. // This is necessary so you never see what is "behind" the navbar. if (st > lastScrollTop && st > navbarHeight){ // Scroll Down $('header').removeClass('nav-down').addClass('nav-up'); } else { // Scroll Up if(st + $(window).height() < $(document).height()) { $('header').removeClass('nav-up').addClass('nav-down'); } } lastScrollTop = st; } 
 body { padding-top: 40px; } header { background: #f5b335; height: 40px; position: fixed; top: 0; transition: top 0.2s ease-in-out; width: 100%; } .nav-up { top: -40px; } main { background:url( data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAPklEQVQYV2O8dOnSfwYg0NPTYwTRuAAj0QqxmYBNM1briFaIzRbi3UiRZ75uNgUHGbfvabgfsHqGaIXYPAMAD8wgC/DOrZ4AAAAASUVORK5CYII= ) repeat; height: 2000px; } footer { background: #ddd;} * { color: transparent} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header class="nav-down"> This is your menu. </header> <main> This is your body. </main> <footer> This is your footer. </footer> 

You can use this:

var mousewheelevt = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x
window.addEventListener(mousewheelevt, function(e){

var evt = window.event || e //equalize event object     
evt = evt.originalEvent ? evt.originalEvent : evt; //convert to  originalEvent if possible               
var delta = evt.detail ? evt.detail*(-40) : evt.wheelDelta //check for detail first, because it is used by Opera and FF

  if(delta > 0) {
    document.getElementById('menu').style.display = "block";
  }
  else{
    if(window.pageYOffset > 250)
       document.getElementById('menu').style.display = "none";
  }   
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM