<template>
<view v-if="visible" class="marquee-root">
<!-- 横向滚动 -->
<view class="marquee-root--row">
<view
class="marquee-root--track"
:class="{
'marquee-root--marquee-seamless': scroll && seamless,
'marquee-root--marquee-non-seamless': scroll && !seamless,
scrolling: scroll,
}"
:style="getTrackStyle()"
>
<view
v-for="(chunk, chunkIdx) in topChunks"
:key="'top-' + chunkIdx"
class="marquee-root--content-chunk"
>
<view
v-for="(item, idx) in mergedList"
:key="'top-' + chunkIdx + '-' + idx"
class="marquee-root--bullet-item"
>
<slot :item="item" :index="idx">
<text class="marquee-root--bullet-text">{{ item.title }}</text>
</slot>
</view>
</view>
</view>
</view>
</view>
</template>
<script setup>
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
const props = defineProps({
/** 数据数组;单项字段由外层插槽自行使用(如 title、avatar、name) */
list: {
type: Array,
default: () => [],
},
/** 是否开启滚动 */
scroll: {
type: Boolean,
default: true,
},
/** 是否无缝滚动(默认 false,无缝时会复制一份内容避免循环时跳动) */
seamless: {
type: Boolean,
default: false,
},
/** 滚动速度,数值越大滚动越快,单位可理解为 px/s */
speed: {
type: Number,
default: 60,
},
/** 是否显示组件,不显示时不渲染以优化性能 */
visible: {
type: Boolean,
default: true,
},
/** 每列 item 间距(px) */
itemGap: {
type: Number,
default: 24,
},
})
// 内部合并后的列表,用于增量追加新数据
const mergedList = ref([])
// 无缝滚动时需要两份内容,非无缝时单份(循环会有可见跳动)
const topChunks = computed(() => (props.seamless ? [1, 2] : [1]))
// 动画状态控制
const isAnimating = ref(true)
// 动画时长与样式
const getTrackStyle = () => {
if (!props.scroll || !isAnimating.value) {
return {
animationDuration: '0s',
animationPlayState: 'paused',
'--item-gap': `${props.itemGap}px`,
}
}
return {
animationDuration: `${800 / Math.max(1, props.speed)}s`,
animationPlayState: 'running',
'--item-gap': `${props.itemGap}px`,
}
}
// 仅在 visible 时同步数据,避免不可见时不必要的计算
watch(
() => [props.list, props.visible],
([newList, visible]) => {
if (!visible) return
if (!newList?.length) return
//syncList(newList)
mergedList.value = [...newList]
},
{ immediate: true, deep: true }
)
onMounted(() => {
//if (props.visible) syncList(props.list)
if (props.visible) {
isAnimating.value = true
}
})
onUnmounted(() => {
// 组件卸载时停止动画,避免内存泄漏和优化性能
isAnimating.value = false
})
</script>
<style lang="less">
.marquee-root {
width: 100%;
height: 100%;
overflow: hidden;
&--row{
height: 100%;
overflow: hidden;
}
&--track {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
width: max-content;
height: 100%;
align-items: center;
will-change: transform;
white-space: nowrap;
&.marquee-root--marquee-seamless {
animation: scroll-seamless linear infinite;
}
&.marquee-root--marquee-non-seamless {
animation: scroll-non-seamless linear infinite;
}
}
&--content-chunk {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
height: 100%;
flex-shrink: 0;
}
/* 黑色块:avatar 在前 + content 在后 */
&--bullet-item {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
height: 100%;
flex-shrink: 0;
margin-right: var(--item-gap, 24px);
width: auto;
box-sizing: border-box;
}
&--bullet-text {
flex: 1;
line-height: 24px;
color: #0070bb;
font-size: 24px;
}
}
@keyframes scroll-seamless {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-50%);
}
}
@keyframes scroll-non-seamless {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-100%);
}
}
</style>
跑马灯组件vue3+es6
xujing_062026-03-30 11:42
相关推荐
随风一样自由1 小时前
【前端独角兽】刚进入前端领域的前端开发用jQuery还是Vue3?IMPYLH2 小时前
HTML 的 <data> 元素YHHLAI2 小时前
[特殊字符] 面试中的 Promise —— 从入门到精通weixin_BYSJ19873 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954沪飘大军3 小时前
ll-llm:一个零依赖、可插拔的 OpenAI 兼容 LLM 代理kyriewen4 小时前
我扒了 GPT-5.6 的全部编程跑分——有一项 OpenAI 没敢放出来前端H5 小时前
微前端 v3 架构升级:从加载隔离到状态协同山河木马5 小时前
GPU自动处理专题3-NDC怎么映射成屏幕像素坐标(视口变换)陆枫Larry6 小时前
小程序包体积优化:用 SVG 图片替换 iconfont,并保留 CSS 控色能力Appthing6 小时前
在 TanStack Start 中使用 VitePWA 的正确配置