Vue3使用Swiper实现列表内容分两行循环滚动

下载Swiper,我这里用的是8.1.1版本

npm 复制代码
//最新版
npm install swiper
//8.1.1版本
npm install swiper@8.1.1

Vue代码

vue 复制代码
<template>
<swiper :slides-per-view="2" :autoplay="{ delay: 3000, disableOnInteraction: false }" :speed="500" :direction="'vertical'" :scrollbar="{ draggable: false }" :loop="true" :modules="sayData.modules" class="swiperclass">
    <swiper-slide class="nr" v-for="(item, index) in sayData.list" :key="item.id">
        <div :title="item.warnMsg" class="flexAlign" style="height: 100%;">
            <span class="normal">{{item.createTime}} {{ item.warnMsg }}</span>
        </div>
    </swiper-slide>
</swiper>
</template>
<script setup>
    import { Swiper, SwiperSlide } from 'swiper/vue';
    import { Autoplay } from 'swiper';
    import 'swiper/css';
    const sayData = ref({
        list: [],
        modules: [Autoplay],
    });
</script>

<style>
    .swiperclass{
        height: 100px;
    }
    .nr{
        height: 50px!important;
        margin-bottom: 0!important;
        cursor: pointer;
    }
</style>

:slides-per-view="2"

  • 含义:设置每页显示的滑块数量。
  • 作用:在这个例子中,每页会显示 2 个滑块(例如图片或卡片)。

2. :autoplay="{ delay: 3000, disableOnInteraction: false }"

  • 含义:启用自动播放功能。

    • delay: 3000:自动切换到下一页的延迟时间,单位是毫秒。这里设置为 3000 毫秒(即 3 秒)。
    • disableOnInteraction: false:是否在用户交互(如滑动)后禁用自动播放。设置为 false 表示即使用户滑动后,自动播放仍然会继续。

3. :speed="500"

  • 含义:滑动动画的速度。
  • 作用:设置滑动动画的持续时间,单位是毫秒。这里设置为 500 毫秒,表示滑动动画会在 0.5 秒内完成。

4. :direction="'vertical'"

  • 含义:滑动的方向。
  • 作用 :设置为 'vertical' 表示滑动方向是垂直的,即上下滑动。如果设置为 'horizontal',则表示水平滑动。

5. :scrollbar="{ draggable: false }"

  • 含义:配置滚动条的样式和行为。

    • draggable: false:是否允许用户通过拖动滚动条来切换滑块。设置为 false 表示用户不能通过拖动滚动条来切换,滚动条仅用于显示当前的滑动位置。

6. :loop="true"

  • 含义:是否启用循环模式。
  • 作用 :设置为 true 表示滑动到最后一张后会自动跳转到第一张,形成一个循环。如果设置为 false,则滑动到最后一张后无法继续滑动。

7. :modules="sayData.modules"

  • 含义:引入 Swiper 的模块。
  • 作用 :sayData.modules 是一个数组,包含了需要使用的 Swiper 模块。Swiper 通过模块化设计,允许按需加载功能模块,以减少代码体积。例如,如果需要使用自动播放功能,就需要引入 Autoplay 模块。 slides-per-view:表示页面默认展示多少条数据

完

相关推荐
搬砖的小码农_Sky16 分钟前
AI Agent:Windows上Node.js安装后 npm -v报错
前端·npm·node.js·人机交互
FYKJ_201028 分钟前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark
计算机魔术师36 分钟前
半年翻倍!OpenAI再融300亿,估值冲到1.4万亿
前端
研☆香1 小时前
jQuery + Swiper.js 实现动态渲染轮播图
前端·javascript·jquery
子兮曰1 小时前
Node.js 50个优势场景盘点:一个人单干,为啥我多数时候只用它
前端·后端
IT_陈寒1 小时前
我花3小时debug,就因为JavaScript这个隐式转换坑
前端·人工智能·后端
excel2 小时前
Nuxt 中 useSeoMeta 与 useHead 的区别
前端
竹林8182 小时前
OmniGame 技术白皮书:从零依赖到 WebRTC P2P,重新定义网页小游戏的工程上限
前端·浏览器
ss2732 小时前
AI全栈实战 | 2.1-02 CSS 布局:垂直居中为什么是经典面试题?所有布局在解决什么矛盾
前端·css
律宏阔2 小时前
Flutter Riverpod 3:统一打印 Provider 错误与自动重试日志
前端·flutter