Skip to content

Instantly share code, notes, and snippets.

@timstl
Last active December 5, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timstl/fbe86aa00239a7e07274 to your computer and use it in GitHub Desktop.
Save timstl/fbe86aa00239a7e07274 to your computer and use it in GitHub Desktop.
jquery.backtotop.js
/*
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