let resizeObserver = null
const containerRef = ref(null)
const containerWidth = ref(0)
// 根据容器宽度计算每页数量
const itemsPerPage = computed(() => {
const w = containerWidth.value
console.log(w)
if (w >= 930) return 4 // 每个卡片约 220px * 4 + 间距
if (w >= 720) return 3 // 220 * 3
if (w >= 480) return 2 // 220 * 2
return 1
})
const groupedItems = computed(() => {
const perPage = itemsPerPage.value
const groups = []
for (let i = 0; i < timelineItems.length; i += perPage) {
groups.push(timelineItems.slice(i, i + perPage))
}
console.log(groups);
return groups
})
// 计算每个卡片的宽度百分比
const cardWidth = computed(() => {
const perPage = itemsPerPage.value
return `${(100 / perPage).toFixed(4)}%` // 如 "25.0000%", "33.3333%"
})
onMounted(() => {
if (!containerRef.value) return
nextTick(() => {
// 创建 ResizeObserver 监听容器宽度
resizeObserver = new ResizeObserver((entries) => {
for (let entry of entries) {
// 获取 content box 宽度(不含 border/padding)
containerWidth.value = Math.floor(entry.contentRect.width)
}
})
resizeObserver.observe(containerRef.value)
})
})
onBeforeUnmount(() => {
if (resizeObserver) {
resizeObserver.disconnect()
}
})
<div ref="containerRef" class="max-w-[930px] mx-auto">
<ElCarousel height="112px" :autoplay="false" indicator-position="none" :arrow="groupedItems.length <= 1 ? 'never' : 'hover'">
<ElCarouselItem v-for="(i, k) of groupedItems" :key="k">
<div class="shortcut max-w-[940px] flex flex-start mb-5 mx-[-5px]">
<div
v-for="(item, index) in i"
:key="index"
class="item mx-[5px] min-w-[190px]"
@click="item.action && item.action()"
:class="{ 'cursor-pointer': item.action }"
:style="{ width: cardWidth }"
>
<div class="flex justify-between mb-2">
<div class="date">{{ item.date }}</div>
<div class="subTitle">{{ item.title }}</div>
</div>
<Ellipsis :lines="2" :line-height="22" class="relative z-10">
<span class="text-[14px]">{{ item.description }}</span>
</Ellipsis>
</div>
</div>
</ElCarouselItem>
</ElCarousel>
</div>
.shortcut{
.item{
width: 220px;
height: 112px;
padding: 16px 20px;
border-radius: 20px;
border-width: 1px;
background: #FFFFFF;
cursor: pointer;
position: relative;
&::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url(../../assets/haisum/home/shortcut-bg.png) center center no-repeat;
background-size: cover;
opacity: 0;
transition: opacity 0.3s ease;
border-radius: 20px;
}
&:hover::before {
opacity: 1;
}
.subTitle{
height: 30px;
padding: 4px 6px;
gap: 4px;
border-radius: 6px;
font-size: 14px;
color: #303133;
background: #F0F2F5;
font-weight: 500;
}
.date{
color: #606266;
font-weight: 500;
font-size: 12px;
line-height: 30px;
padding-left: 20px;
background: url(../../assets/haisum/home/vector.svg) left center no-repeat;
}
}
}
ElCarousel 动态走马灯
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 《人生走马灯》是一部2025年播出的热门短剧,通过独特的叙事风格和深刻的剧情,展现了人生百态与情感纠葛。该剧以都市...
- 现实主义情感悬疑短剧《人生走马灯》以独特的叙事视角和情感张力引发关注,在传统都市情感剧框架中融入悬疑推理与哲学思辨...
- 2025年3月28日,一部名为《人生走马灯》的短剧悄然上线,却在短时间内引发观剧热潮。这部由导演阳光执导、陆枭与祝...
- 提示:短剧《人生走马灯》完整版在线观看地址百度云/夸克网盘资源链接放在文章中间👇👇,往下翻就行 《人生走马灯》:在...
- 《人生走马灯》是一部充满情感张力与心理深度的现代都市剧,讲述了一位女性在经历离婚、丧失与自我认知的复杂心路历程后,...