原生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中的每个项目占满视口高度,页面可以通过自动或手动方式平滑滚动这些项目。
相关推荐
早點睡3901 分钟前
ReactNative项目OpenHarmony三方库集成实战:react-native-collapsible
javascript·react native·react.js
前端Hardy14 分钟前
别再手写代码了!2026 前端 5 个 AI 杀招,直接解放 80% 重复劳动(附工具+步骤)
前端·javascript·面试
SuperEugene17 分钟前
Element Plus/VXE-Table UI 组件库规范:统一用法实战,避开样式冲突与维护混乱|工程化与协作规范篇
前端·javascript·vue.js·ui·前端框架·element plus·vxetable
前端Hardy32 分钟前
前端工程师必备的 10 个 AI 万能提示词(Prompt),复制直接用,效率再翻倍!
前端·javascript·面试
BioRunYiXue36 分钟前
Nature Methods:CellVoyager 自主 AI 智能体开启生物数据分析新时代
大数据·开发语言·前端·javascript·人工智能·数据挖掘·数据分析
再玉米地里吃过亏1 小时前
ONENET平台API鉴权错误
前端
网络点点滴1 小时前
Vue3中Suspense的使用
前端·javascript·vue.js
饼干哥哥2 小时前
搭建一个云端Skills系统,随时随地记录TikTok爆款
前端·后端
酉鬼女又兒2 小时前
零基础快速入门前端Web存储(sessionStorage & localStorage)知识点详解与蓝桥杯考点应用(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·html
DanCheOo2 小时前
# 从"会用 AI"到"架构 AI":高级前端的认知升级
前端·ai编程