HTML Preparation code:
x
 
1
<style>
2
    body {
3
      margin: 0;
4
      background-color: #f1f1f1;
5
      font-family: Arial, Helvetica, sans-serif;
6
    }
7
    
8
    #navbar {
9
      background-color: #333;
10
      position: fixed;
11
      top: 0;
12
      width: 100%;
13
      display: block;
14
      transition: top 0.3s;
15
    }
16
    
17
    #navbar a {
18
      float: left;
19
      display: block;
20
      color: #f2f2f2;
21
      text-align: center;
22
      padding: 15px;
23
      text-decoration: none;
24
      font-size: 17px;
25
    }
26
    
27
    #navbar a:hover {
28
      background-color: #ddd;
29
      color: black;
30
    }
31
    .hide-menu{
32
        transform: translateY(110%)!important;
33
    }
34
    .show-menu{
35
        transform: translateY(0%)!important;
36
    }
37
</style>
38
</head>
39
40
<body>
41
42
    <div id="navbar">
43
        <a href="#home">Home</a>
44
        <a href="#news">News</a>
45
        <a href="#contact">Contact</a>
46
    </div>
47
48
    <div style="padding:15px 15px 2500px;font-size:30px;margin-top:30px;">
49
        <p><b>This example demonstrates how to hide a navbar when the user starts to scroll the page.</b></p>
50
        <p>Scroll down this frame to see the effect!</p>
51
        <p>Scroll up to show the navbar.</p>
52
        <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
53
        <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
54
    </div>
Tests:
  • 1

     
    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 = "-50px";
      }
      prevScrollpos = currentScrollPos;
    }
  • 2

     
    var prevScrollpos = window.pageYOffset;
    var navbar = document.getElementById("footer-menu");
    var delta = 0;
    var didScroll;
    // on scroll, let the interval function know the user has scrolled
    window.onscroll = function() {
      didScroll = true;
    }
    // run hasScrolled() and reset didScroll status
    setInterval(function() {
      if (didScroll) {
        hasScrolled();
        didScroll = false;
      }
    }, 500);
    function hasScrolled() {
        var last_direction = delta;
      var currentScrollPos = window.pageYOffset;
      delta = prevScrollpos - currentScrollPos;
      console.log(delta);
      
      if (Math.sign(last_direction) != Math.sign(delta))
      {
          if (delta > 0) {
            navbar.classList.add("show-menu");
            navbar.classList.remove("hide-menu");
            console.log('up');
          } 
          
          else if (delta < 0) {
            navbar.classList.remove("show-menu");
            navbar.classList.add("hide-menu");  
            console.log('down');        
          }
      }
      prevScrollpos = currentScrollPos;
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    1
    2

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Chrome 92 on Windows
View result in a separate tab
Test name Executions per second
1 288093.4 Ops/sec
2 111225.4 Ops/sec