Created
October 23, 2020 03:56
-
-
Save danro/7df62a6f0832e351bb11e4cf804af7de to your computer and use it in GitHub Desktop.
Auto-scroll the browser to a clicked tabs element position
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> | |
// 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