[code="js"]
;(function($){ //开始前加分号是为了兼容性;此处$作为匿名函数的形参
//返回顶部
$.fn.toTop = function(options){
var options=$.extend({bottom:"30%",right:100,showHeight:20},options);
this.css("bottom",options.bottom);
this.css("right",options.right+'px');
this.css("visibility","hidden");
$(this).addClass('top topStyle');
$this=$(this);
$(window).scroll(function(){
if($(window).scrollTop>=options.showHeight)$this.css("visibility","visible");
else $this.css("visibility","hidden");
});
$(this).click(function(){
$(window).scroll(0);
});
return this;
}
})(jQuery)
[/code]
[code="html"]
<!DocType html>
置顶
<br> $(function(){<br> $("#divToTop").toTop();<br> });<br>
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
这是内容
[/code]
[code="css"]
.topStyle
{
background-color: transparent;
cursor: pointer;
position: fixed;
}
.top
{
background: url(img/gotop.png);
height: 48px;
width: 48px;
background-position: 102px 48px;
}
.top:hover
{
background: url(img/gotop.png);
height: 48px;
width: 48px;
background-position: 102px 0px;
}
[/code]
当滚动时