Skip to content

Instantly share code, notes, and snippets.

@memetican
Created November 11, 2023 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memetican/e253b8b5d4e3a70779633f0754e4bc40 to your computer and use it in GitHub Desktop.
Save memetican/e253b8b5d4e3a70779633f0754e4bc40 to your computer and use it in GitHub Desktop.
Smooth scroll
<style>
html {
scroll-behavior: smooth;
}
</style>
<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