/** * plugin name: jquery totop for smoothly scroll back to top * plugin url: https://github.com/mmkjony/jquery.totop * version: 1.1 * author: mmk jony * author url: https://github.com/mmkjony * license: licensed under mit **/ (function( $ ){ 'use strict'; $.fn.totop = function(opt){ //variables var elem = this; var win = $(window); var doc = $('html, body'); //extended options var options = $.extend({ autohide: true, offset: 420, speed: 500, position: true, right: 15, bottom: 30 }, opt); elem.css({ 'cursor': 'pointer' }); if(options.autohide){ elem.css('display', 'none'); } if(options.position){ elem.css({ 'position': 'fixed', 'right': options.right, 'bottom': options.bottom, }); } elem.click(function(){ doc.animate({scrolltop: 0}, options.speed); }); win.scroll(function(){ var scrolling = win.scrolltop(); if(options.autohide){ if(scrolling > options.offset){ elem.fadein(options.speed); } else elem.fadeout(options.speed); } }); }; }( jquery ));