Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function scrollToTop() {
- var scrollToTopButton = document.getElementById( 'tg-scroll-to-top' );
- /* Only proceed when the button is present. */
- if ( scrollToTopButton ) {
- /* On scroll and show and hide button. */
- window.addEventListener(
- 'scroll',
- function () {
- if ( 500 < window.scrollY ) {
- scrollToTopButton.classList.add( 'tg-scroll-to-top--show' );
- } else if ( 500 > window.scrollY ) {
- scrollToTopButton.classList.remove(
- 'tg-scroll-to-top--show'
- );
- }
- }
- );
- /* Scroll to top when clicked on button. */
- scrollToTopButton.addEventListener(
- 'click',
- function ( e ) {
- e.preventDefault();
- /* Only scroll to top if we are not in top */
- if ( 0 !== window.scrollY ) {
- window.scrollTo(
- {
- top : 0,
- behavior: 'smooth'
- }
- );
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment