当前位置: 首页 > news >正文

滚动显示效果

  • 1效果

  • 自动滚动消息列表(上下循环);

  • 鼠标移入暂停滚动,移出继续;

  • 每个 item 有 hover 效果(右移 + 背景变浅);

  • 使用了“复制一份 list”实现无缝循环。

  • .scroll-content 是整个滚动区域。

  • transform: translateY(${scrollY}px) 控制滚动位置。

  • 当滚动到第一份数据末尾时,通过复制的第二份 list实现无缝衔接。

  • 当滚动超过第一份高度时重置 scrollY

  • 2✅ 完整代码示例

    <template><div class="list-boxs"><divclass="scroll-container"ref="scrollContainer"@mouseenter="stopScroll"@mouseleave="startScroll"><divclass="scroll-content":style="{ transform: `translateY(${scrollY}px)` }"><!-- 第一份数据 --><div class="item" v-for="item in list" :key="item.id" @click="goMessages(item)"><div class="top"><img src="/src/assets/images/messages.svg" alt="" /><div class="name-box">{{ item.name }}</div></div><div class="time">{{ item.time }}</div></div><!-- 第二份复制数据实现无缝滚动 --><div class="item" v-for="item in list" :key="'copy-' + item.id" @click="goMessages(item)"><div class="top"><img src="/src/assets/images/messages.svg" alt="" /><div class="name-box">{{ item.name }}</div></div><div class="time">{{ item.time }}</div></div></div></div></div>
    </template><script setup lang="ts">
    import { ref, onMounted, onBeforeUnmount } from 'vue';const scrollContainer = ref<HTMLElement | null>(null);
    const scrollY = ref(0);
    const speed = 0.4; // 滚动速度(越大越快)
    let paused = false;
    let animationFrameId: number | null = null;// 模拟数据
    const list = ref([{ id: 1, name: '系统更新公告:版本1.2上线', time: '2025-10-20 09:00' },{ id: 2, name: '任务提醒:项目A进度待确认', time: '2025-10-20 10:30' },{ id: 3, name: '审批结果:系统B已通过审核', time: '2025-10-20 11:15' },{ id: 4, name: '数据同步完成:2025年10月', time: '2025-10-20 12:00' },{ id: 5, name: '消息通知:您有一条新留言', time: '2025-10-20 13:00' },
    ]);const step = () => {if (!paused) {scrollY.value -= speed;const contentHeight = scrollContainer.value?.scrollHeight || 0;const containerHeight = scrollContainer.value?.clientHeight || 0;// 到底后重置,实现无缝循环if (Math.abs(scrollY.value) >= contentHeight / 2) {scrollY.value = 0;}}animationFrameId = requestAnimationFrame(step);
    };onMounted(() => {animationFrameId = requestAnimationFrame(step);
    });onBeforeUnmount(() => {if (animationFrameId) cancelAnimationFrame(animationFrameId);
    });function stopScroll() {paused = true;
    }function startScroll() {paused = false;
    }function goMessages(item: any) {console.log('点击消息:', item);
    }
    </script><style scoped lang="less">
    .list-boxs {height: calc(100% - 46px);margin-top: 25px;.scroll-container {position: relative;height: 100%;overflow: hidden;// 渐隐效果:上下边缘淡出mask-image: linear-gradient(to bottom, transparent, black 10%, black 90%, transparent);-webkit-mask-image: linear-gradient(to bottom, transparent, black 10%, black 90%, transparent);.scroll-content {will-change: transform;}}.item {height: 74px;padding: 12px;border-radius: 8px;background: #f3f9fd;cursor: pointer;transition: transform 0.3s ease, background 0.3s ease;&:not(:last-child) {margin-bottom: 12px;}&:hover {transform: translateX(5px);background: #e6f2fa;}.top {display: flex;align-items: center;img {flex-shrink: 0;width: 16px;height: 16px;margin-right: 7px;}.name-box {width: calc(100% - 23px);overflow: hidden;color: #425974;font-size: 14px;font-weight: 400;line-height: 23px;text-align: left;text-overflow: ellipsis;white-space: nowrap;}}.time {margin-top: 4px;margin-left: 23px;color: #8e97a2;font-size: 14px;font-weight: 400;line-height: 23px;text-align: left;}}
    }
    </style>
    

http://www.dtcms.com/a/506909.html

相关文章:

  • Spring Cloud - Spring Cloud 微服务概述 (微服务的产生与特点、微服务的优缺点、微服务设计原则、微服务架构的核心组件)
  • YOLOv4:目标检测领域的 “速度与精度平衡大师”
  • agent设计模式:第二章节—路由
  • 玩转Docker | 使用Docker安装uptime-kuma监控工具
  • flutter开发小结
  • 【运维】鲲鹏麒麟V10 操作系统aarch64自制OpenSSH 9.8p1 rpm包 ssh漏洞修复
  • react学习(五) ---- hooks
  • 【C语言】程序的编译和链接(基础向)
  • 基于单片机的热量计测量系统设计
  • 显卡功能及原理介绍
  • 丽水网站建设明恩玉杰百度网址导航
  • 时序数据库选型指南:从大数据视角看IoTDB的核心优势
  • 免费域名网站的网站后台用什么做
  • HTML应用指南:利用GET请求获取全国沃尔沃门店位置信息
  • WPF/C#:使用Microsoft Agent Framework框架创建一个带有审批功能的终端Agent
  • 『 QT 』信号-槽 补充: Qt信号槽断开连接与Lambda槽技巧
  • 为何海尔洗衣机屡屡发明新品类?
  • 【案例实战】HarmonyOS应用性能优化实战案例
  • 企业网站建设尚未实现宣传功能交互效果好的移动端网站
  • 10m带宽做下载网站深圳一公司今年新成立16家核检机构
  • 优麒麟(Ubuntu Kylin) 安装向日葵远程工具/ToDesk
  • 速卖通新号优惠券采购:自养号效率提升的安全要点
  • Linux InfiniBand代理模块深度解析:管理数据包的高效处理引擎
  • 开源协作2.0:GitHub Discussions+AI重构开发者社区的知识共创生态
  • Linux01:基础指令与相关知识铺垫(一)
  • QueryWrapper - MyBatis-Plus的“查询条件构建器“
  • Linux外设驱动开发1 - 单总线驱动开发__dht11
  • 使用高性能流式的库SpreadCheetah来添加图片和合并表格单元
  • 建设银行网站建设情况免费招聘的网站
  • 手机上怎么做微电影网站徐州做网站谁家最专业