原生js写数据自动纵向滚动,鼠标移入后停止滚动可手动滚动,鼠标移出转自动

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>滚动页面</title>
  <link rel="stylesheet" href="styles.css">
  <style>
    body,
    html {
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

    #scrollContainer {
      width: 100%;
      height: 100vh;
      overflow: hidden;
      position: relative;
    }

    #scrollContent {
      display: flex;
      flex-direction: column;
      transition: transform 0.3s ease;
    }

    .item {
      width: 100%;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 5em;
      color: white;
    }

    .item:nth-child(odd) {
      background-color: #333;
    }

    .item:nth-child(even) {
      background-color: #666;
    }
  </style>
</head>

<body>
  <div id="scrollContainer">
    <div id="scrollContent">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
      <div class="item">5</div>
    </div>
  </div>
</body>

<script>
  const scrollContainer = document.getElementById('scrollContainer');
  const scrollContent = document.getElementById('scrollContent');
  
  let currentPosition = 0;
  let intervalId = null;
  const scrollDistance = 2;
  const itemHeight = scrollContent.clientHeight / scrollContent.children.length;
  const maxScrollPosition = 0;
  const minScrollPosition = -itemHeight * (scrollContent.children.length - 1);

  function autoScroll() {
    if (currentPosition <= minScrollPosition) {
      currentPosition = 0;
    }
    scrollContent.style.transform = `translateY(${currentPosition}px)`;
    currentPosition -= scrollDistance;
  }

  function startAutoScroll() {
    intervalId = setInterval(autoScroll, 20);
  }

  function stopAutoScroll() {
    clearInterval(intervalId);
  }

  function handleWheel(event) {
    currentPosition -= event.deltaY;
    if (currentPosition > maxScrollPosition) {
      currentPosition = maxScrollPosition;
    } else if (currentPosition < minScrollPosition) {
      currentPosition = minScrollPosition;
    }
    scrollContent.style.transform = `translateY(${currentPosition}px)`;
  }

  scrollContainer.addEventListener('mouseenter', function () {
    stopAutoScroll();
    scrollContainer.addEventListener('wheel', handleWheel);
  });

  scrollContainer.addEventListener('mouseleave', function () {
    scrollContainer.removeEventListener('wheel', handleWheel);
    startAutoScroll();
  });

  startAutoScroll();
</script>

</html>
  • 初始化变量(currentPositionintervalIdscrollDistanceitemHeightmaxScrollPositionminScrollPosition),用于跟踪滚动状态和动画。
  • autoScroll()函数持续地将scrollContent向上移动(translateY),直到达到minScrollPosition,然后重置。
  • startAutoScroll()stopAutoScroll()函数通过setIntervalclearInterval控制自动滚动动画。
  • handleWheel(event)函数根据鼠标滚轮事件(wheel)调整currentPosition,确保滚动保持在maxScrollPositionminScrollPosition定义的范围内。
  • scrollContainer上的事件监听器(mouseentermouseleave)触发自动滚动停止/开始,并启用/禁用通过鼠标滚轮进行手动滚动。
  • mouseenter时,自动滚动停止,并且可以通过鼠标滚轮进行手动滚动。
  • mouseleave时,手动滚动停止,并且自动滚动恢复。
  • #scrollContent中的每个项目占满视口高度,页面可以通过自动或手动方式平滑滚动这些项目。
相关推荐
anOnion9 小时前
构建无障碍组件之Menu Button pattern
前端·html·交互设计
用户479492835691510 小时前
claude Fable用不了?把Gpt 5.5pro接到你的claude code里
前端·后端
JieE21210 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
冬奇Lab12 小时前
AI Workflow 定义的四次演进:从 Markdown 到 JS 脚本,再到分布式多 Agent
javascript·人工智能·agent
zhangxingchao12 小时前
Kotlin常用的Flow 操作符整理
前端
IT_陈寒14 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
Pedantic15 小时前
SwiftUI 手势笔记
前端·后端
橙子家15 小时前
浏览器缓存之【结构化数据库与缓存】: IndexedDB、Cache storage 和 Storage buckets
前端
user205855615181316 小时前
X6 中边悬浮置顶,规避 `mouseleave` 事件丢失问题
前端
李明卫杭州16 小时前
CSS aspect-ratio 属性完全指南
前端