码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-06-22 21:54:16      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:z-index   部分   anim   pos   alt   定位   logs   font   tom   

效果如下:

技术分享

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

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

标签:z-index   部分   anim   pos   alt   定位   logs   font   tom   

原文地址:http://www.cnblogs.com/Murloc/p/7066971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!