Last active
December 5, 2018 21:39
-
-
Save timstl/fbe86aa00239a7e07274 to your computer and use it in GitHub Desktop.
jquery.backtotop.js
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
/* | |
back to top | |
usage: $('#main').backtotop(); | |
use localScroll plugin for smooth scrolling | |
style in CSS. | |
*/ | |
;(function($) { | |
$.fn.backtotop = function(options) { | |
var opts = $.extend({}, $.fn.backtotop.defaults, options), | |
$this = this, | |
$win = $(window); | |
$this.each(function() { | |
var $b2t = $this.find('.b2t'), | |
id = $this.attr('id'); | |
if ($b2t.length <= 0) { | |
$this.append('<a href="#' + id + '" title="' + opts.text + '" class="b2t">' + opts.text + '</a>'); | |
$b2t = $this.find('.b2t'); | |
$b2t.on('click', function() { $(this).blur(); }); | |
} | |
function b2tcheck() { | |
( $win.scrollTop() > opts.offset ) ? $b2t.addClass('b2t-show') : $b2t.removeClass('b2t-show'); | |
} | |
$win.on('scroll', b2tcheck); | |
b2tcheck(); | |
}); | |
}; | |
// default options | |
$.fn.backtotop.defaults = { | |
offset: 300, | |
text: "Return to top" | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment