vue3中swiper在loop模式下切换到倒数第二个后,最后一个旁边是空白的怎么解决?

作者站长头像
站长
· 阅读数 11

使用了swiper的coverflowEffect效果,设置了同时显示3个,同时开启了loop模式,

当切换到倒数第二张时没有新的slider生成,导致最后一张的右边是空白的,只有切换到最后一张时才会再在后面复制新的slider,怎么让它在显示倒数第二张时就生成新的slider?vue3中swiper在loop模式下切换到倒数第二个后,最后一个旁边是空白的怎么解决?

这是代码

<template>
    <swiper
style=" width: 800px;height: 169px;"
        loop
        navigation
        effect="coverflow"
        :coverflowEffect="{
            rotate: 0,
            stretch: 0,
            depth: 200,
            modifier: 2,
            slideShadows: false
        }"
        :autoplay="{
            delay: 2000,
            stopOnLastSlide: false,
            disableOnInteraction: false
        }"
        :modules="modules"
        :slides-per-view="3"
        :space-between="-30"
        :pagination="{ clickable: true }"
        @swiper="onSwiper"
        @slideChange="onSlideChange"
    >
        <swiper-slide v-for="i in 3" :key="i">
            <div
                style="
                    width: 300px;
                    height: 169px;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                "
                :style="{ backgroundColor: color16() }"
            >
                {{ i }}
            </div>
        </swiper-slide>
    </swiper>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import {
    Navigation,
    EffectCoverflow,
} from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/less';
import 'swiper/less/navigation';
import 'swiper/less/effect-coverflow';

const color16 = () => {
    //十六进制颜色随机
    const r = Math.floor(Math.random() * 256);
    const g = Math.floor(Math.random() * 256);
    const b = Math.floor(Math.random() * 256);
    const color = `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
    return color;
};

const modules = ref([
    Navigation,
    EffectCoverflow,
]);

const onSwiper = (swiper: any) => {
    console.log(swiper);
};
const onSlideChange = () => {
    console.log('slide change');
};
</script>

<style lang="less" scoped></style>
回复
1个回答
avatar
test
2024-07-14

最后发现slidesPerView设置为auto,就正常了

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容