一键置顶速记

<!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">
    <title>Document</title>
    <script src="http://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
    <style>
        .box {
            position: fixed;
            right: 10px;
            bottom: 10px;
            height: 30px;
            width: 50px;
            text-align: center;
            padding-top: 20px;
            background-color: lightblue;
            border-radius: 20%;
            overflow: hidden;
        }

        .box:hover:before {
            top: 50%
        }

        .box:hover .box-in {
            visibility: hidden;
        }

        .box:before {
            position: absolute;
            top: -50%;
            left: 50%;
            transform: translate(-50%, -50%);
            content: '回到顶部';
            width: 40px;
            color: peru;
            font-weight: bold;
        }

        .box-in {
            visibility: visible;
            display: inline-block;
            height: 20px;
            width: 20px;
            border: 3px solid black;
            border-color: white transparent transparent white;
            transform: rotate(45deg);
        }
    </style>
</head>


<body style="height:2000px;">
    <div>我是顶部</div>
    <div id="box" class="box">
        <div class="box-in"></div>
    </div>
</body>
<script>
    var timer = null;
    box.onclick = function () {
        cancelAnimationFrame(timer);
        timer = requestAnimationFrame(function fn() {
            var oTop = document.body.scrollTop || document.documentElement.scrollTop;
            if (oTop > 0) {
                document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
                timer = requestAnimationFrame(fn);
            } else {
                cancelAnimationFrame(timer);
            }
        });
    }

</script>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值