img

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

2023-04-14 0条评论 656次阅读 JavaScript


image.png

<!DOCTYPE html>
<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>

💬 COMMENT


🦄 支持markdown语法

👋友