首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ JavaScript / 文章详情

js 当滚动到某个元素的底部时触发打印

Sonder
2023-04-14
1255字
3分钟
浏览 (1k)

image.png
复制代码
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
        }
        .header {
            height: 600px;
            background-color: #333;
            color: #fff;
        }
        .article {
            height: 1000px;
            background-color: #f2f2f2;
        }
        .bottom {
            height: 500px;
            background-color: #ccc;
        }
    </style>
</head>
<body>
<div class="header">Header</div>
<div class="article">Article</div>
<div class="bottom">Bottom</div>
<script>
  let header = document.querySelector('.header');

  window.addEventListener('scroll', function() {
    let headerBottom = header.offsetTop + header.offsetHeight;
    let scrollTop = window.pageYOffset || document.documentElement.scrollTop;

    if (scrollTop >= headerBottom) {
      console.log('已经滚动到头部底部');
    }
  });
</script>
</body>
</html>
下一篇 / js 将list转化为tree格式的几种写法

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)