返回顶部按钮 js普通函数版/class构造函数版/JQ版

本文的返回顶部 有两种版本:①js普通函数版; ②class构造函数版;③JQuery版

话不多说直接上代码!(其中样式较简陋,引用时可以自己根据需要来修改样式)

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>01backTop</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            height: 3000px;
        }

        .goTop {
            width: 30px;
            height: 30px;
            background-color: rgb(0, 183, 255);
            position: fixed;
            right: 50px;
            bottom: 50px;

            display: none;
        }
    </style>
</head>

<body>
    <div class="goTop"></div>

    <script>
        /*
        * @show 为滚动到哪里开始显示出置顶按钮
        * @speed 为返回顶部的速度(数值越大速度越快,20-60为最佳值,过快会影响体验感)
        */
        function backTop(show=500,speed=30) {
            // 定义一个置顶按钮载体
            const goTop = document.querySelector(".goTop")

            // 置顶按钮的隐藏/显示
            window.onscroll = function () {
                if (window.scrollY > show) {
                    goTop.style.display = "block"
                } else {
                    goTop.style.display = "none"
                }
            }

            // 绑定置顶时间,并在置顶后清除间隔定时器
            goTop.onclick = function () {
                let timeID = setInterval(
                    function () {
                        document.documentElement.scrollTop -= speed
                        console.log(document.documentElement.scrollTop);

                        if (document.documentElement.scrollTop === 0) {
                            clearInterval(timeID)
                        }
                    }, 10
                )
            }
        }
        backTop()
    </script>
</body>

</html>

Class构造函数 封装的返回顶部 版本

  这是引用class构造函数的示范html代码

<!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>01backTop_test</title>
    <script src="./01backTop_park.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            height: 3000px;
        }

        .goTop {
            width: 30px;
            height: 30px;
            background-color: rgb(0, 183, 255);
            position: fixed;
            right: 50px;
            bottom: 50px;

            display: none;
        }
    </style>
</head>
<body>
    <div class="goTop"></div>
    <script>
        const goTop = document.querySelector(".goTop")
        new BackTop(goTop,600,50)
    </script>
</body>
</html>

 这是Class构造函数的 js 代码

class BackTop {
    constructor(domRoot, show, speed) {
        this.goTop = domRoot
        this.show = show
        this.speed = speed
        this.scrollEvent()
        this.bindEvent()
    }

    // 置顶按钮的隐藏/显示
    scrollEvent() {
        window.onscroll = () => {
            if (window.scrollY > this.show) {
                goTop.style.display = "block"
            } else {
                goTop.style.display = "none"
            }
        }
    }

    bindEvent() {
        goTop.onclick = () => {
            let timeID = setInterval(
                () => {
                    document.documentElement.scrollTop -= this.speed
                    if (document.documentElement.scrollTop === 0) {
                        clearInterval(timeID)
                    }
                }, 10
            )
        }
    }
}

JQuery版本(该版本较简单,不做过多解释)

<!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>返回顶部——JQuery版</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            height: 3000px;
        }

        .goTop {
            width: 30px;
            height: 30px;
            background-color: pink;
            position: fixed;
            right: 50px;
            bottom: 50px;

            display: none;
        }
    </style>
</head>

<body>
    <div class="goTop"></div>

    <script src="../jquery/jquery-3.6.0.js"></script>

    <script>
        $(window).scroll(function () {
            if ($(this).scrollTop() > 500) {
                $(".goTop").css({ display: "block" })
            } else {
                $(".goTop").css({ display: "none" })
            }
        })

        $(".goTop").click(function () {
            $("html").animate(
                { scrollTop: 0 },
                500,
                () => console.log("动画完成")
            )
        })

    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值