jQuery返回顶部的按钮

时间:2022-03-30 10:05:33

 

HTML结构:

<script type="text/javascript" src="../js/jquery-1.8.3/jquery.js"></script>

body里的代码:


<
div class="hulubo"> <a href="javascript:"></a>
</div>

css结构:

.hulubo{left:1180px;
bottom
:69.8px;
right
:auto;
position
:fixed;
z-index
: 199999;}
.hulubo a
{position:absolute;
height
:58px;width:58px;
background
:url(../images/gotop.png)no-repeat scroll;background-position:center;background-size: 58px 58px;}

直接改宽高和背景图片就可以了

jQuery代码:

 1 $(function(){  
2 var $btn_top = $('.hulubo');
3 12 $("body").append($btn_top);
13 $("body").on("click",".hulubo",function(){
14 $("html,body").animate({scrollTop: 0},"slow");
15 });
16 var $btn = $(".hulubo");
17 if($(window).scrollTop() > 100){
18 $btn.fadeIn(600);
19 }
20 $(window).scroll(function(){
21 if($(window).scrollTop() > 100){
22 $btn.fadeIn(600);
23 }else{
24 $btn.fadeOut(600);
25 }
26 });
27 });