js 页面缓慢移动到顶部,移动到底部实现

<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<title>SCROLL</title>
<style type="text/css">
</style>
<script type="text/javascript">
    var goToWhere = function (where)
    {
        var me = this;
        me.site = [];
        me.sleep = me.sleep ? me.sleep : 26;
        me.fx = me.fx ? me.fx : 26;
        clearInterval (me.interval);
        var dh = document.documentElement.scrollHeight || document.body.scrollHeight;
        var height = !!where ? dh : 0;
        me.interval = setInterval (function ()
        {
            var top = document.documentElement.scrollTop || document.body.scrollTop;
            var speed = (height - top) / me.fx;
            if (speed === me.site[0])
            {
                window.scrollTo (0, height);
                clearInterval (me.interval);
            }
            window.scrollBy (0, speed);
            me.site.unshift (speed);
        }, me.sleep);
    };
</script>
</head>
<body>
    <div style="height: 1000px; text-align: center; font-size: 200px; font-weight: bold;">5</div>
    <div style="height: 1000px; text-align: center; font-size: 200px; font-weight: bold;">4</div>
    <div style="height: 1000px; text-align: center; font-size: 200px; font-weight: bold;">3</div>
    <div style="height: 1000px; text-align: center; font-size: 200px; font-weight: bold;">2</div>
    <div style="height: 1000px; text-align: center; font-size: 200px; font-weight: bold;">1</div>
    <div style="height: 1000px; text-align: center; font-size: 200px; font-weight: bold;">0</div>
    <div id="back-up" onclick="goToWhere(0)"
        style="border: 1px solid red; height: 100px; width: 15px; position: fixed; cursor: pointer; right: 10px; bottom: 150px;">返回顶部</div>
    <div id="back-up" onclick="goToWhere(1)"
        style="border: 1px solid red; height: 100px; width: 15px; position: fixed; cursor: pointer; right: 10px; bottom: 30px;">返回底部</div>
</body>
</html>

在这里插入图片描述
goToWhere(0) -----------------------> 返回顶部
goToWhere(1) -----------------------> 返回底部

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值