banner
banner
banner
NEWS LETTER

uni-app 无限滚动组件与 CSS 无缝滚动

Scroll down

一个「无限滚动」能力在这份代码里走了两步:先在同目录的 test.html 里用纯 CSS 把动画调通,再试图封成 infiniteScroll 组件挂到「动画练习地」空壳页上。
原型部分很值得学:overflow: hidden 裁剪 + translate3d 位移 + linear 匀速无限动画 + 内容首尾复制,四件事凑齐就是无缝滚动。
组件部分目前只是骨架:direct 的方向没被消费,动画被注释掉,props 还被同名 ref 遮蔽,页面看起来「能跑」,实际渲染的一直是组件内的假数据。
下面先讲组件壳的结构,再回头讲 CSS 原型,最后集中收四处已知问题——每一处都写成「问题 → 修正」。

一、组件壳:横纵向无限滚动容器

「动画练习地」页面本体只有 13 行,职责就是把组件挂上去:

1
2
3
4
5
6
7
8
9
<template>
<view>
<InfiniteScroll direct="false"></InfiniteScroll>
</view>
</template>

<script setup lang="ts">
import InfiniteScroll from '/components/infiniteScroll.vue'
</script>

注意两点:导入路径用的是源码根绝对路径 /components/...,而不是项目里其他页面常用的 @/components/...;两者都能解析,但同一个项目里最好只约定一种。

组件内部的结构是「一层裁剪 + 一层方向容器 + N 张卡片」:

1
2
3
4
5
6
7
8
9
<template>
<view class="scroll-container">
<view :class="direct ? 'column' : 'horizon'">
<view class="card" v-for="(item, index) in list" :key="index" mark:currentindex="index">
<view>{{item}}</view>
</view>
</view>
</view>
</template>

direct 只负责在 .horizon(横排)和 .column(纵排)之间切 class,滚动本身完全交给 CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
.scroll-container {
position: relative;
overflow: hidden; /* 裁掉溢出,是「滚动带」的前提 */
width: 100%;
.horizon { display: flex; } /* 横排 */
.column { display: flex; flex-direction: column; } /* 纵排 */
.card {
width: 100px;
border: 1px solid white;
background-color: green;
margin: 0 10px;
// animation: scaleImg 6s linear infinite; /* 动画被注释掉了 */
}
}

@keyframes scrollBoxH {
0% { transform: translateX(0) }
100% { transform: translateX(-300%); } /* 横向滚动关键帧 */
}

@keyframes scaleImg {
0% { transform: scale(0.8) }
50% { transform: scale(1); }
100% { transform: scale(0.8); }
}

脚本里 defineProps 声明了 list / direct 两个入参,但同一作用域下又写了同名的 const list / const direct:

1
2
3
4
5
6
7
8
9
import { ref } from 'vue'

const list = ref(['1','2','3','4','5','6','7','8','1','2']) // ← 与 props.list 同名
const direct = ref(false) // true: 水平方向,false: 垂直方向 // ← 与 props.direct 同名

defineProps({
list: Array,
direct: Boolean,
})

这直接导致父页传进来的值全部失效,细节放在第三节。

二、纯 CSS 无缝滚动原型(keyframes + 首尾复制)

test.html 是可以独立在浏览器打开的静态页,它是组件的「设计稿」:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<style>
@keyframes rowup {
0% {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
100% {
-webkit-transform: translate3d(-100px, 0, 0);
transform: translate3d(-100px, 0, 0);
}
}
.list {
width: 300px;
border: 1px solid #999;
margin: 20px auto;
position: relative;
height: 200px;
overflow: hidden; /* 裁掉溢出 */
}
.list .rowup {
-webkit-animation: 10s rowup linear infinite normal;
animation: 10s rowup linear infinite normal;
position: relative;
}
</style>
1
2
3
4
5
6
7
8
<div class="list">
<div class="cc rowup">
<div class="item">1- 121233fffffr</div>
<div class="item">2- 3123233</div>
<!-- 内容重复到足够长,保证位移一段后仍有内容接上 -->
<div class="item">19- 国资委原</div>
</div>
</div>

拆开看只有三个要点:

  1. 容器裁剪:.list 设 overflow: hidden,把滚动带之外的部分藏起来;
  2. 匀速动画:animation: 10s rowup linear infinite normal,linear 是关键,用 ease 会让循环衔接处「顿一下」;
  3. 只位移一屏:100% 只从 0 移到 -100px(约等于单份内容的宽度),配合内容复制,视觉上就无限。
组成 作用 常见错误
overflow: hidden 裁掉滚动带外的内容 忘了加,内容溢出到页面外
animation ... linear infinite 匀速、无限循环 用 ease / ease-in-out,接缝处每轮变速
首尾内容复制 滚过一份后无缝接上 只放一份内容,滚到末尾露底
translate3d(x,0,0) 触发 GPU 合成层,滚动更顺 用 translateX,长列表偶发抖动

位移距离必须等于单份内容的总宽度。原型里的 -100px 是随手写的示例值,真实使用时按内容宽度算,否则接缝处会出现空白或跳格。

三、踩坑(问题 → 修正)

① direct="false" 把字符串传给了 Boolean prop

问题:页面写 <InfiniteScroll direct="false">,组件声明 direct: Boolean。Vue 对 Boolean prop 的约定是「除了 false 字面量之外的非空字符串都转成 true」——但这里传的是字符串 "false",direct 实际拿到 true,语义完全反了。

修正:需要布尔值时用 :direct="false" 绑定表达式;只写 direct="false" 传的是字符串,布尔语义不会被解析。

② props 与本地 ref 同名,父组件的值被静默吞掉

问题:const list = ref([...]) / const direct = ref(false) 和 defineProps({ list, direct }) 同名。<script setup> 里模板绑定优先解析本地作用域,所以模板里的 list / direct 永远指本地 ref——父页传的 direct 被覆盖,props.list 一次都没用上,而且不报错、不警告。

修正:把本地变量改名(const innerList = ref(...)),或干脆删掉假数据、模板里统一用 props.list。凡是「组件始终显示同一份数据」,先查有没有同名遮蔽。

③ key="index" 漏了冒号

问题:v-for="(item, index) in list" key="index" 传给 key 的是字符串 "index",所有项共用同一个字面量 key,列表增删时会复用错节点、状态错乱。

修正:写成 :key="index"。更稳妥的是用数据自身的唯一 id,index 只在静态列表里勉强可用。

④ 关键帧定义了但 animation 被注释,组件「看着静止」

问题:scrollBoxH 和 scaleImg 两段 @keyframes 都写好了,但 .card 上的 animation 是注释状态,于是这个组件永远不会动——很容易被误判成「组件逻辑没写对」。

修正:在 .card(或方向容器)上启用 animation: scrollBoxH 6s linear infinite,并确认位移距离与单份内容宽度一致。

小结

真正的可复用资产是 test.html 那套 translate3d + linear + infinite + overflow: hidden + 内容复制 的组合,跑马灯、公告轮播、无缝列表都靠它打底。
组件壳的思路(overflow: hidden 裁剪 + :class 切方向 + v-for 出卡片)是对的,缺的是「接真实数据 + 启用动画 + 修正 prop 类型」这三步。
四处问题里,最该记住的是 props 与本地 ref 同名导致的静默失效——它不报错,只是让你绑定的不是你以为的那份数据。

其他文章
cover
uni-app 组件通信与评论卡片
  • 26/12/02
  • 10:00
  • 微信小程序
目录导航 置顶
  1. 1. 一、组件壳:横纵向无限滚动容器
  2. 2. 二、纯 CSS 无缝滚动原型(keyframes + 首尾复制)
  3. 3. 三、踩坑(问题 → 修正)
  4. 4. 小结
请输入关键词进行搜索