Skip to content

Instantly share code, notes, and snippets.

@lgmcolin
Created January 19, 2014 02:14
Show Gist options
  • Save lgmcolin/8499584 to your computer and use it in GitHub Desktop.
Save lgmcolin/8499584 to your computer and use it in GitHub Desktop.
jquery Go-top插件
(function($){
$.GoToTop = {
defaults: {
autoShow : true,
timeEffect : 500,
effectScroll : 'linear',
appearMethod : 'slide'
},
init:function(options){
opts = $.extend({}, $.GoToTop.defaults, options),
$.GoToTop._constructLink();
if(opts.autoShow)
$(window).scroll(function(){
if($(this).scrollTop() != 0) {
switch (opts.appearMethod) {
case 'fade' : divBack.fadeIn('fast'); break;
case 'slide' : divBack.slideDown('fast'); break;
default : divBack.show();
}
}
else {
switch (opts.appearMethod) {
case 'fade' : divBack.fadeOut('fast'); break;
case 'slide' : divBack.slideUp('fast'); break;
default : divBack.hide();
}
}
});
$('#GoToTop').click(function(e) {
e.preventDefault();
$('body,html').animate({scrollTop:0},opts.timeEffect,opts.effectScroll);
});
},
_constructLink:function() {
divBack = $('<a />',{
id : 'GoToTop',
href : '#body',
style : 'float: left; display: none; position: fixed; bottom: 40px; right: 0px; z-index: 90000; height: 60px; width: 60px; background-image: url("images/GoToTop.png"); background-repeat: no-repeat;'
}).prependTo('body');
if(!opts.autoShow) divBack.show();
}
};
GoToTop = function(options) {
$.GoToTop.init(options);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment