-
-
Save DevTarakanova/1d5447a252ad402b6aca194af03f9f4e to your computer and use it in GitHub Desktop.
Sticky Header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.sticky { | |
position: fixed; | |
top: 0; | |
width: 100%; | |
} | |
.sticky + .content { | |
padding-top: 102px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="header" id="header"> | |
<h2>My Header</h2> | |
</div> | |
<div class="content"> | |
<h3>On Scroll Sticky Header</h3> | |
<p>The header will stick to the top when you reach its scroll position.</p> | |
<p>Scroll back up to remove the sticky effect.</p> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
window.onscroll = function() {myFunction()}; | |
var header = document.getElementById("header"); | |
var sticky = header.offsetTop; | |
function myFunction() { | |
if (window.pageYOffset > sticky) { | |
header.classList.add("sticky"); | |
} else { | |
header.classList.remove("sticky"); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment