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权限失败了?。

转载于:https://www.cnblogs.com/Murloc/p/7066971.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值