Skip to content

Instantly share code, notes, and snippets.

@giventofly
Last active December 11, 2023 06:53
Show Gist options
  • Save giventofly/5c20d49d3894fc28dfc2ceeb7cfee9b9 to your computer and use it in GitHub Desktop.
Save giventofly/5c20d49d3894fc28dfc2ceeb7cfee9b9 to your computer and use it in GitHub Desktop.
scroll to element vanilla js
//from https://css-tricks.com/snippets/jquery/smooth-scrolling/
// Scroll to specific values
// scrollTo is the same
window.scroll({
top: 2500,
left: 0,
behavior: 'smooth'
});
// Scroll certain amounts from current position
window.scrollBy({
top: 100, // could be negative value
left: 0,
behavior: 'smooth'
});
// Scroll to a certain element
document.querySelector('.hello').scrollIntoView({
behavior: 'smooth'
});
//jQuery way
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment