0

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>
John
  • 13
  • 5
  • Why without jquery when you have jquery codes? – Farzin Kanzi Mar 25 '17 at 15:30
  • I just mentioned in the description, because speed optimization and slow internet connection – John Mar 25 '17 at 15:32
  • Yes i know, But there is no different in speed when you already have jquery in your code. – Farzin Kanzi Mar 25 '17 at 15:36
  • As a matter of fact, it has some impact. For example if I defer jquery the script doesn't work anymore and the page is getting a little bit slower because the browser loads all the page including scripts and just after start to render the page. That is my issue. – John Mar 25 '17 at 15:42

1 Answers1

0

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";
  }   
});
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23
  • Thank you Farzin. Indeed it works. If want to activate it after 250px scrolling down? – John Mar 25 '17 at 16:09
  • You can set a condition `if(window.scrollY > 250)` or `if(window.pageYOffset > 250)` – Farzin Kanzi Mar 25 '17 at 16:13
  • I added the if statement. – Farzin Kanzi Mar 25 '17 at 16:18
  • for adding some transition I need to change .style.display = "none" whit .add.Class('some-class') and also .remove.Class(' ') right? – John Mar 25 '17 at 16:24
  • No addClass/removeClass are jquery functions. To work with classes read this: http://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript – Farzin Kanzi Mar 25 '17 at 16:28
  • But i do not think it is related to your transitions. If you want it fadeOut/fadeIn gradually, toggle its opacity to 0/1 and set `transition-duration: 1s;` – Farzin Kanzi Mar 25 '17 at 16:29
  • @John. if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Farzin Kanzi Mar 25 '17 at 17:21
  • thank you for you help, yes I check it. I was out a little bit and now I'm back. I'll do read more and again thank you for your helping me out. – John Mar 25 '17 at 17:46
  • Hi @Farzin, just tested on my android note 2 device the code you provided but does not work. Do you have any idea why? – John Mar 28 '17 at 13:37
  • Sorry my friend, I don't know.. But you can ask this as another question and you will get your answer. But in your question mention the name of browser that has problem. – Farzin Kanzi Mar 28 '17 at 20:45