Last active
December 2, 2015 07:23
-
-
Save Buooy/cd9876481a8cc19aab5b to your computer and use it in GitHub Desktop.
jQuery script to add a back to top button. Requires jQuery and Font Awesome
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
| jQuery.fn.backtotop = function( options ) { | |
| // This is the easiest way to have default options. | |
| var settings = jQuery.extend({ | |
| color: "white", | |
| backgroundColor: "rgba(200,200,200,0.75)", | |
| visibleAfter: "100px", | |
| left : "15px", | |
| right: "", | |
| marginLeft : "0px", | |
| bottom : "20px", | |
| size : "40px", | |
| speed : 700, | |
| }, options ); | |
| var style = ""; | |
| if( settings.right == "" ){ | |
| style += "left:"+settings.left+";"; | |
| }else{ | |
| style += "right:"+settings.right+";"; | |
| } | |
| style += "margin-left"+settings.marginLeft+";"; | |
| style += "bottom:"+settings.bottom+";"; | |
| style += "line-height:"+settings.size+";"; | |
| style += "border-radius:"+settings.size+";height:"+settings.size+";width:"+settings.size+";"; | |
| style += "background-color:"+settings.backgroundColor+";"; | |
| style += "color:"+settings.color+";" | |
| style += "cursor: pointer;display: none;text-align:center;position:fixed;"; | |
| // Back to Top on Click | |
| var back_to_bottom_button = jQuery("<div class='back-to-top' style='"+style+"'><i class='fa fa-arrow-up'></i></div>"); | |
| back_to_bottom_button.click(function(e){ | |
| e.preventDefault(); | |
| jQuery('html,body').animate({ | |
| scrollTop: 0 | |
| }, settings.speed); | |
| }); | |
| // Fades in at a certain point | |
| jQuery(document).scroll(function(){ | |
| if( jQuery(document).scrollTop() > parseInt(settings.visibleAfter) ){ | |
| back_to_bottom_button.fadeIn(); | |
| }else{ | |
| back_to_bottom_button.fadeOut(); | |
| } | |
| }); | |
| this.append( back_to_bottom_button ); | |
| return this; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment