<style>
body {
margin: 0;
background-color: #f1f1f1;
font-family: Arial, Helvetica, sans-serif;
}
#navbar {
background-color: #333;
position: fixed;
top: 0;
width: 100%;
display: block;
transition: top 0.3s;
}
#navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 15px;
text-decoration: none;
font-size: 17px;
}
#navbar a:hover {
background-color: #ddd;
color: black;
}
.hide-menu{
transform: translateY(110%)!important;
}
.show-menu{
transform: translateY(0%)!important;
}
</style>
</head>
<body>
<div id="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
</div>
<div style="padding:15px 15px 2500px;font-size:30px;margin-top:30px;">
<p><b>This example demonstrates how to hide a navbar when the user starts to scroll the page.</b></p>
<p>Scroll down this frame to see the effect!</p>
<p>Scroll up to show the navbar.</p>
<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>
<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>
</div>
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;
}
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;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 |
Test name | Executions per second |
---|---|
1 | 288093.4 Ops/sec |
2 | 111225.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition Json
The provided JSON represents a benchmark definition for a test case that measures the performance of hiding and showing a navbar when the user scrolls down or up a web page. The script preparation code sets up an HTML structure with a navbar and some text content, while the Html Preparation Code
defines the CSS styles for the navbar.
Individual Test Cases
There are two individual test cases:
window.pageYOffset
property to track the scroll position and updates the navbar's top
CSS style property accordingly.show-menu
and hide-menu
) accordingly.Library and Syntax
Both test cases use JavaScript, but Test Case 2 introduces a custom function called hasScrolled()
. This function is not part of any widely used library, so it appears to be a custom implementation for detecting changes in scroll direction.
Comparison Options
The two test cases compare the performance of:
Pros and Cons
Test Case 1:
Test Case 2:
hasScrolled()
function and interval loop.Other Considerations
Overall, these two test cases demonstrate different approaches to measuring the performance of hiding and showing a navbar when scrolling. While Test Case 1 is simple and straightforward, Test Case 2 introduces additional complexity but potentially more accurate results.