js回到顶部

回到顶部

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    div {
        width: 300px;
        height: 800px;
        background: linear-gradient(#FFF, #f45, #f67);
    }

    button {
        position: fixed;
        bottom: 10px;
    }
</style>

<body>
    <div>
        <button id="but">回到顶部</button>
    </div>

    <script>
        document.getElementById('but').onclick = function () {
            document.body.scrollTop = 0;
            document.documentElement.scrollTop = 0;
        }
    </script>
</body>

</html>
posted @ 2021-11-12 17:27  江咏之  阅读(53)  评论(0编辑  收藏  举报