图标或标签随着滚动条移动,到达一定高度后停止移动(Scroll)

具体可以用下面代码看效果

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="author" content="苏七的小情绪">
    <title></title>
    <style>
        .top {
            width: 100%;
            height: 500px;
            background-color: lightblue;
        }

        .scroll {
            position: relative;
        }

        .con {
            height: 300px;
            background-color: lightgrey;
        }

        .move {
            position: absolute;
            bottom: 0;
            right: 100px;
            width: 100px;
            height: 100px;
            background-color: lightcoral;
        }

        .bottom {
            height: 200px;
            background-color: lightblue;
        }
    </style>
</head>

<body>
    <div class="top"></div>
    <div class="scroll">
        <div class="con"></div>
        <div class="move"></div>
    </div>
    <div class="bottom"></div>
</body>

</html>
<script>
    // 1400是top+scroll的总高度,我们设定scroll的最大高度为900,所以这里500+900
    // 500是scroll上面内容的高度
    function a(){
        var div = document.querySelector('.scroll')
        var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        var height = 1400 - h;
        setTimeout(function () {
                if (scrollTop < height) {
                    console.log(h,scrollTop);
                    div.style.height=  (h - 500 + scrollTop) + 'px';
                } else {
                    div.style.height=  '900px';
                }
        }, 50)
    };
    a();
    window.addEventListener('scroll', function () {
        a()
    });
</script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值