html 通过滚动回到顶部

16 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.suoluomei.com/common/js/jquery-2.1.4.min.js"></script>
    <title>Document</title>
</head>
<style>
    .srccem{
        height: 9999px;
        width: 100%;
            margin: 0;
            background-color: white;
    }
    .returntop {
        position: fixed;
        bottom: 5px;
        right: 5px;
    }

    .returntop_img {
        width: 50px;
    }
</style>

<body>
        <div class="srccem"> 
        <div class="returntop">
                <img src="https://sucai.suoluomei.cn/sucai_zs/images/20191126182210-top.png" class="returntop_img">
          </div>
  

    </div>
</body>
<script>

    
        $(function(){
            $(".returntop").hide();
                    //当滚动条的位置处于距顶部300像素以下时,跳转链接出现,否则消失
                        $(window).scroll(function () {
                            if ($(window).scrollTop() > 300) {
                                $(".returntop").fadeIn(500);
                            } else {
                                $(".returntop").fadeOut(500);
                            }
                        });
                        //滑动回到顶部 点击事件
            $('.returntop').click(function(){
                $('body,html').animate({'scrollTop':0}, 700);
            })
        })
        </script>

</html>

首先,在html中,用到了两个div元素,给他们加上class(或者id)
然后我们设置srccem的高度,设高一点,这样就会产生滚动条
然后设置returntop,让它固定不变的在右下角
滑动特效要用到jQuery插件
然后我们再给returntop设定一个点击事件,设定方法如下

`   $('.returntop').click(function(){
                $('body,html').animate({'scrollTop':0}, 700);
            })
        })

为了让returntop滑动之后显示,所以要加一个方法

$(".returntop").hide();
                    //当滚动条的位置处于距顶部600像素以下时,跳转链接出现,否则消失
                        $(window).scroll(function () {
                            if ($(window).scrollTop() > 300) {
                                $(".returntop").fadeIn(500);
                            } else {
                                $(".returntop").fadeOut(500);
                            }
                        });

两个方法代码

<script>

    
        $(function(){
            $(".returntop").hide();
                    //当滚动条的位置处于距顶部600像素以下时,跳转链接出现,否则消失
                        $(window).scroll(function () {
                            if ($(window).scrollTop() > 300) {
                                $(".returntop").fadeIn(500);
                            } else {
                                $(".returntop").fadeOut(500);
                            }
                        });
                        //滑动回到顶部 点击事件
            $('.returntop').click(function(){
                $('body,html').animate({'scrollTop':0}, 700);
            })
        })
        </script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值