if(window.jQuery===undefined){
throw new Error("此插件依赖于jQuery,请引入jQuery库");
}
;(function($){
$.fn.blacktop=function(opt){
var defaults={ //用于写插件的配置参数
spend:600,
position:'true',
right:30,
bottom:30,
autohide:'true',
offset:$(window).height()
};
var option=$.extend(defaults,opt);
//this.each(function(){
//var _this=$(this); //如果用this.each()的话此时this要写成$(this)
var _this=this;
var win = $(window);
if(option.autohide){
_this.css({
'display':'none',
'cursor':'pointer'
});
}
if(option.position){
_this.css({
'position': 'fixed',
'right': option.right+'px',
'bottom': option.bottom+'px',
});
}
_this.click(function(){
$('html, body').animate({scrollTop: 0}, option.spend);
});
win.scroll(function(){
var scrolling = win.scrollTop();
if(option.autohide){
if(scrolling > option.offset){
_this.fadeIn(option.spend);
}
else _this.fadeOut(option.spend);
}
});
//});
return this;
};
//调用: $('xxx').blacktop({可以传你想要更改的参数}).css('background','red');