实现返回顶部按钮一直在页面底部(兼容ie6)

本文介绍了一种使用jQuery来实现在网页滚动时显示并定位到页面顶部的功能的方法。通过监听窗口滚动事件调整按钮的位置,并在点击按钮时将页面平滑滚动到顶部。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript" src="./jquery-1.8.0.js"></script>
    <style type="text/css">
        .back_top {width: 100px; height: 100px; border: 1px solid purple; text-align: center; line-height: 100px; display: none;}
        .change {position: absolute; left: 600px; display: block; cursor: pointer;}
    </style>
</head>
<body>
    <div style="width:500px; height:2200px; border:1px solid red; margin:0 auto; position:relative;">
        <div style="margin-top:10px; border:1px solid blue;">a</div>
        <div style="margin-top:2140px; border:1px solid blue;">b</div>

        <div class="back_top">
            返回顶部
        </div>
    </div>

    <script type="text/javascript">
        $(window).resize(function() {
            $('.back_top').css('bottom', $(document.body).height() - $(window).scrollTop() - $(window).height()  + 10);
        });

        $(window).scroll(function() {
            $('.back_top').addClass('change');
            $('.back_top').css('bottom', $(document.body).height() - $(window).scrollTop() - $(window).height() + 10);

            if($(window).scrollTop() === 0) {
                $('.back_top').removeClass('change');
            }
        });

        $('.back_top').on('click', function() {
            $(window).scrollTop(0);
        });
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值