Skip to content

Instantly share code, notes, and snippets.

@danro
Created October 23, 2020 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danro/7df62a6f0832e351bb11e4cf804af7de to your computer and use it in GitHub Desktop.
Save danro/7df62a6f0832e351bb11e4cf804af7de to your computer and use it in GitHub Desktop.
Auto-scroll the browser to a clicked tabs element position
<script>
// When a tab is clicked, animate scroll to the top of the tabs element
$('.w-tab-link').on('click', function (event) {
var clickedLink = $(event.currentTarget);
var tabsElement = clickedLink.closest('.w-tabs');
var rootElement = $('html, body');
var newTop = tabsElement.offset().top;
if (rootElement.scrollTop() !== newTop) {
rootElement.stop().animate({scrollTop: newTop}, 500, 'swing');
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment