Vue3-实现无线轮播
·

<template>
<div class="container">
<div class="marquee">
<div
class="track"
:style="{
animationDuration: speed + 's',
animationPlayState: isPaused ? 'paused' : 'running',
}"
>
<template v-for="(img, i) in list2" :key="i">
<img
:src="img"
class="item"
@mouseenter="isPaused = true"
@mouseleave="isPaused = false"
/>
<p>
{{ i }}
</p>
</template>
</div>
</div>
</div>
</template>
<script setup>
import { computed, ref } from "vue";
const list = ref([
"https://picsum.photos/200/120?1",
"https://picsum.photos/200/120?2",
"https://picsum.photos/200/120?3",
"https://picsum.photos/200/120?4",
]);
const speed = ref(10);
// 双数组实现无缝滚动
const list2 = computed(() => [...list.value, ...list.value]);
// 是否暂停
const isPaused = ref(false);
</script>
<style scoped>
.container {
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.marquee {
width: 500px;
overflow: hidden;
white-space: nowrap;
}
.track {
display: inline-flex;
animation: scroll linear infinite;
}
.item {
width: 140px;
margin-right: 12px;
object-fit: cover;
cursor: pointer;
}
@keyframes scroll {
from {
transform: translateX(0);
}
to {
transform: translateX(-50%);
}
}
</style>
更多推荐


所有评论(0)