Created
November 11, 2023 20:55
-
-
Save memetican/e253b8b5d4e3a70779633f0754e4bc40 to your computer and use it in GitHub Desktop.
Smooth scroll
This file contains hidden or 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
<style> | |
html { | |
scroll-behavior: smooth; | |
} | |
</style> |
This file contains hidden or 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> | |
document.addEventListener("DOMContentLoaded", () => { | |
// Check if the URL contains a hash | |
const { hash } = window.location; | |
if (hash) { | |
// Try to find the element | |
const target = document.getElementById(hash.slice(1)); | |
if (target) { | |
// Scroll to the element smoothly | |
target.scrollIntoView({ behavior: "smooth" }); | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment