JQuery实现淡入淡出的回到顶部按钮

效果如下:

按钮的html和css代码如下。

<div id="gotop">
    <i class="fa fa-chevron-up"></i>
</div>

i标签<i class="fa fa-chevron-up"></i>使用了Font Awesome,400多个图标(包括QQ、微信、微博)只要30KB,可无限放大,不需要js,非常的🐮。

按钮gotop用fixed定位到屏幕右下角,并且隐藏。

#gotop {
    position: fixed;
    right: 10px;
    bottom: 12%;
    z-index: 666;
    display: none;
    width: 40px;
    height: 40px;
    font-size: 30px;
    line-height: 40px;
    text-align: center;
    color: #fff;
    background-color: #21759b;
    border-radius: 2px;
    cursor: pointer;
}
#gotop:hover {
    background-color: #195a84;
}

JQ代码如下,上半部分检测滚动条位置显示和隐藏gotop按钮,下半部分点击按钮后圆滑的回到顶部。

 1 $(function () {
2 // 保存gotop按钮 3 var gotop = $("#gotop"); 4 // 检测滚动条位置,高于600回顶部按钮显示 5 $(window).scroll(function () { 6 console.log($(window).scrollTop()); 7 if ($(window).scrollTop() > 600) { 8 gotop.fadeIn(1000); 9 } 10 else { 11 gotop.fadeOut(1000); 12 } 13 });
14 // gotop按钮点击,圆滑回到顶部 scrollTop: 0表示到滚动条的0,800(ms)表示用时。 15 gotop.click(function () { 16 $('body,html').animate({ scrollTop: 0 }, 800);18 });
19 });

 没有特效的按钮可以看我的博客右下角,博客园申请JS权限失败了😭。

posted @ 2017-06-22 21:01  超能鱼人  阅读(679)  评论(0编辑  收藏  举报
测试一下