Created
September 28, 2017 14:39
-
-
Save Netzberufler/7d9425bc362ed2b35f09ee5c1ee8aee2 to your computer and use it in GitHub Desktop.
Scroll to Top
This file contains 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
/** | |
* Scroll to Top JS | |
*/ | |
( function( $ ) { | |
$.fn.scrollToTop = function() { | |
var scrollButton = $( this ); | |
/* Hide Button by default */ | |
scrollButton.hide(); | |
/* Show Button on scroll down */ | |
var showButton = function() { | |
var window_top = $( window ).scrollTop(); | |
if ( window_top > 150 ) { | |
scrollButton.fadeIn( 200 ); | |
} else { | |
scrollButton.fadeOut( 200 ); | |
} | |
} | |
showButton(); | |
$( window ).scroll( showButton ); | |
/* Scroll back to top when element is clicked */ | |
scrollButton.click( function () { | |
$( 'html, body' ).animate( { scrollTop: 0 }, 300 ); | |
return false; | |
} ); | |
}; | |
$( document ).ready( function() { | |
/* Add Button to HTML DOM */ | |
$( 'body' ).append( '<button id=\"scroll-to-top\" class=\"scroll-to-top-button\"></button>' ); | |
/* Add Scroll To Top Functionality */ | |
$( '#scroll-to-top' ).scrollToTop(); | |
} ); | |
} ( jQuery ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment