Vanilla JS sticky header
How do I make an vanilla js sticky header?
What is a vanilla js sticky header? How do you make a vanilla js sticky header? This script and codes were developed by Eli Fitch on 28 October 2022, Friday.
Vanilla JS sticky header - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Vanilla JS sticky header</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <body> <div class="hero"></div> <div id="header"></div> <div id="readout"></div>
</body> <script src="js/index.js"></script>
</body>
</html>
Vanilla JS sticky header - Script Codes CSS Codes
* { margin: 0; padding: 0;
}
html { background-color: gray; height: 5000px;
}
.hero { height: 200px; background-color: blue; display: block;
}
#header { height: 75px; display: block; background: green; width: 100%;
}
#readout { background-color: white; position: fixed; z-index: 100; bottom: 0; right: 0; width: 200px; height: 50px;
}
Vanilla JS sticky header - Script Codes JS Codes
var h = document.getElementById("header");
var readout = document.getElementById("readout");
var stuck = false;
var stickPoint = getDistance();
function getDistance() { var topDist = h.offsetTop; return topDist;
}
window.onscroll = function(e) { var distance = getDistance() - window.pageYOffset; var offset = window.pageYOffset; readout.innerHTML = stickPoint + ' ' + distance + ' ' + offset + ' ' + stuck; if ( (distance <= 0) && !stuck) { h.style.position = 'fixed'; h.style.top = '0px'; stuck = true; } else if (stuck && (offset <= stickPoint)){ h.style.position = 'static'; stuck = false; }
}
Developer | Eli Fitch |
Username | elifitch |
Uploaded | October 28, 2022 |
Rating | 4 |
Size | 1,944 Kb |
Views | 129,536 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Cube of cubes | 6,112 Kb |
Mars | 3,817 Kb |
Smiley guy | 3,845 Kb |
Slanty Button Mixin | 4,109 Kb |
Goodbye 2016 | 5,786 Kb |
Node DC Animated SVG Logo | 4,055 Kb |
Neon slicey cube | 5,020 Kb |
Animated multi-line underline | 3,247 Kb |
3D Retro Wave Spin | 5,825 Kb |
Gradient Borders Mixin | 3,394 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Scifi-style Interative Form | Aaronchuo | 4,566 Kb |
Drag n Drop | Martin42 | 2,594 Kb |
Zeichensatz | Moklick | 2,058 Kb |
Project Euler Problem 17 | Bfillmer | 2,739 Kb |
Mobile Sub Menu Concept | Berdejitendra | 2,790 Kb |
Bootstrap Carousel Fade Transition | Rowno | 2,484 Kb |
Mario | Takaneichinose | 3,902 Kb |
Material design - button rainbow circle | Kunukn | 3,652 Kb |
A Pen by Anoop | Anoopjohn | 330,760 Kb |
Rrremark.com Overlay Highlighter | Derickruiz | 4,438 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!