vue3中使用swiper的方法及版本兼容问题

news/2024/11/15 0:23:11 标签: vue.js, 前端, javascript

Swiper官方文档

https://swiper.com.cn/

前言

如果使用vue3开发尽量避免swiper6及以下版本(踩的坑很多),我使用的swiper7.4.1

开发中vue总是会遇到版本兼容性问题,每次都要调半天,很头疼....废话不多说,直接上方法及代码。

1.首先安装swiper

npm install swiper@7.4.1 -S

yarn add swiper@7.4.1 -S

 2.在页面组件中引入

//按需引入
import { Navigation, Pagination, A11y, Autoplay, Thumbs } from "swiper";

import { Swiper, SwiperSlide } from "swiper/vue";

import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";
import "swiper/css/thumbs";
import "swiper/css/autoplay";

2-1 需要抛出各种各种样式

const modules = [Autoplay, Navigation, Pagination, A11y, Thumbs];

2-2 基础代码

 <div class="swiperbox">
    <div class="swiper-container">
      <swiper
        :slidesPerView="1"
        :spaceBetween="10"
        :loop="true"
        :centeredSlides="true"
        :autoplay="autoplayOptions"
        :navigation="true"
        :thumbs="{ swiper: thumbsSwiper }"
        :modules="modules"
        class="mainSwiper"
      >
        <swiper-slide v-for="item in state.images" :key="item.id">
          <img :src="item.src" alt="" />
        </swiper-slide>
      </swiper>
    </div>
 </div>

2-3 参数说明

slidesPerView轮播图组件中每次显示的幻灯片数量
spaceBetween滑块组件(Swiper)中每个滑块之间的间距
loop是否开启循环(loop)模式
centeredSlides是否居中幻灯片
autoplay是否启动自动切换
navigation前进后退按钮
thumbs用于制作带缩略图的swiper

更多参数及api可参考官方文档

3.完整代码

<template>
  <div class="swiperbox">
    <div class="swiper-container">
      <swiper
        :slidesPerView="1"
        :spaceBetween="10"
        :loop="true"
        :centeredSlides="true"
        :autoplay="autoplayOptions"
        :navigation="true"
        :thumbs="{ swiper: thumbsSwiper }"
        :modules="modules"
        class="mainSwiper"
      >
        <swiper-slide v-for="item in state.images" :key="item.id">
          <img :src="item.src" alt="" />
        </swiper-slide>
      </swiper>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { Navigation, Pagination, A11y, Autoplay, Thumbs } from "swiper";

import { Swiper, SwiperSlide } from "swiper/vue";

import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";
import "swiper/css/thumbs";
import "swiper/css/autoplay";
const mainSwiper = ref(null);
const thumbsSwiper = ref(null);
const activeIndex = ref(1);
const modules = [Autoplay, Navigation, Pagination, A11y, Thumbs];
const autoplayOptions = ref({
  delay: 3000,//自动切换的时间间隔,单位ms
  stopOnLastSlide: false,//如果设置为true,当切换到最后一个slide时停止自动切换。(loop模式下无效)。
  disableOnInteraction: true,//用户操作swiper之后,是否禁止autoplay。默认为true:停止。
});

const state = reactive({
  images: [
    {
      id: 1,
      src: "https://ts1.cn.mm.bing.net/th/id/R-C.1d7122d74918319a0cc94eff2b870412?rik=dsaOP4X%2bD4vC7Q&riu=http%3a%2f%2fwww.deskcar.com%2fdesktop%2ffengjing%2f201384105239%2f8.jpg&ehk=lnWFTxhKyLQmWGeKiv1oI2jRDUPoFgT4ocZcrXbaZXM%3d&risl=&pid=ImgRaw&r=0",
    },
    {
      id: 2,
      src: "https://ts1.cn.mm.bing.net/th/id/R-C.c2553800ddb1015c38246aecd165843c?rik=Y5qhhj7kuO39cg&riu=http%3a%2f%2fimg.1ppt.com%2fuploads%2fallimg%2f2010%2f1_201026214635_2.jpg&ehk=Cm6AeBW%2fCgIyGPPcOdSmqswQvN6d89ezbaUXPVaVuEg%3d&risl=&pid=ImgRaw&r=0",
    },
  ],
});

</script>
<style lang="less" scoped>
.swiperbox {
  height: 100vh;
  position: relative;
  background: black;
  .swiper-container {
    width: 100%;

    .mainSwiper {
      height: 800px;
      // 自定义分页箭头样式(左箭头)
      :deep(.swiper-button-prev) {
        color: #ffffff;
      }
      // 自定义分页箭头样式(右箭头)
      :deep(.swiper-button-next) {
        color: #ffffff;
      }
      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }
  }
}


</style>

效果截图

4.附加(缩略图及改变滑动方向)

4-1 如果想要使用缩略图,可使用api--Thumbs 与缩略Swiper联动

<template>
  <div class="swiperbox">
    <div class="swiper-container">
      <!-- 主图 Swiper -->
      <swiper
        :slidesPerView="1"
        :spaceBetween="10"
        :loop="true"
        :centeredSlides="true"
        :autoplay="autoplayOptions"
        :navigation="true"
        :thumbs="{ swiper: thumbsSwiper }"
        :modules="modules"
        class="mainSwiper"
      >
        <swiper-slide v-for="item in state.images" :key="item.id">
          <img :src="item.src" alt="" />
        </swiper-slide>
      </swiper>
    </div>

    <!-- 缩略图 Swiper -->
    <swiper
      @swiper="setThumbsSwiper" 
      :slidesPerView="2"
      :spaceBetween="10"
      :loop="false"
      :navigation="true"
      :pagination="true"
      :modules="modules"
      class="mySwiper"
    >
      <swiper-slide v-for="item in state.images" :key="item.id">
        <img :src="item.src" alt="" />
      </swiper-slide>
    </swiper>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { Navigation, Pagination, A11y, Autoplay, Thumbs } from "swiper";

import { Swiper, SwiperSlide } from "swiper/vue";

import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/pagination";
import "swiper/css/thumbs";
import "swiper/css/autoplay";
const mainSwiper = ref(null);
const thumbsSwiper = ref(null);
const activeIndex = ref(1);
const modules = [Autoplay, Navigation, Pagination, A11y, Thumbs];
const autoplayOptions = ref({
  delay: 3000,//自动切换的时间间隔,单位ms
  stopOnLastSlide: false,//如果设置为true,当切换到最后一个slide时停止自动切换。(loop模式下无效)。
  disableOnInteraction: true,//用户操作swiper之后,是否禁止autoplay。默认为true:停止。
});

const state = reactive({
  images: [
    {
      id: 1,
      src: "https://ts1.cn.mm.bing.net/th/id/R-C.1d7122d74918319a0cc94eff2b870412?rik=dsaOP4X%2bD4vC7Q&riu=http%3a%2f%2fwww.deskcar.com%2fdesktop%2ffengjing%2f201384105239%2f8.jpg&ehk=lnWFTxhKyLQmWGeKiv1oI2jRDUPoFgT4ocZcrXbaZXM%3d&risl=&pid=ImgRaw&r=0",
    },
    {
      id: 2,
      src: "https://ts1.cn.mm.bing.net/th/id/R-C.c2553800ddb1015c38246aecd165843c?rik=Y5qhhj7kuO39cg&riu=http%3a%2f%2fimg.1ppt.com%2fuploads%2fallimg%2f2010%2f1_201026214635_2.jpg&ehk=Cm6AeBW%2fCgIyGPPcOdSmqswQvN6d89ezbaUXPVaVuEg%3d&risl=&pid=ImgRaw&r=0",
    },
  ],
});

const setThumbsSwiper = (swiper) => {
  thumbsSwiper.value = swiper;
};
</script>
<style lang="less" scoped>
.swiperbox {
  height: 100vh;
  position: relative;
  background: black;
  .swiper-container {
    width: 100%;

    .mainSwiper {
      height: 800px;
      // 自定义分页箭头样式(左箭头)
      :deep(.swiper-button-prev) {
        color: #ffffff;
      }
      // 自定义分页箭头样式(右箭头)
      :deep(.swiper-button-next) {
        color: #ffffff;
      }
      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }
  }
}

.mySwiper {
  width: 300px;
  margin: 10px auto;
  img {
    width: 100px;
    height: 100px;
    object-fit: cover;
    cursor: pointer;
  }

  .swiper-slide {
    opacity: 0.4;
  }
  .swiper-slide-thumb-active {
    opacity: 1;
  }
}
</style>

效果图

4-2 如果想要改动滑动的方向,可以设置direction属性

directionSwiper的滑动方向,可设置为水平方向切换 horizontal 或垂直方向切换 vertical

 代码示例

 <swiper
        :slidesPerView="1"
        :spaceBetween="10"
        :loop="true"
        :centeredSlides="true"
        :autoplay="autoplayOptions"
        :navigation="true"
        :thumbs="{ swiper: thumbsSwiper }"
        :modules="modules"
        class="mainSwiper"
        :direction="'vertical'"
      >
        <swiper-slide v-for="item in state.images" :key="item.id">
          <img :src="item.src" alt="" />
        </swiper-slide>
      </swiper>


http://www.niftyadmin.cn/n/5752498.html

相关文章

游戏引擎学习第三天

视频参考:https://www.bilibili.com/video/BV1XTmqYSEtm/ 之前的程序不能退出&#xff0c;下面写关闭窗体的操作 PostQuitMessage 是 Windows API 中的一个函数&#xff0c;用于向当前线程的消息队列发送一个退出消息。其作用是请求应用程序退出消息循环&#xff0c;通常用于处…

java八股-垃圾回收机制-垃圾回收算法,分代回收,垃圾回收器

文章目录 垃圾回收算法引用计数法可达性分析算法 jvm垃圾回收算法标记清除算法标记整理算法复制算法本章总结 JVM中的分代回收本章总结 JVM有哪些垃圾回收器&#xff1f;1.串行垃圾收集器2.并行垃圾收集器3.CMS&#xff08;并发&#xff09;垃圾收集器本章小结 详细聊一下G1垃圾…

在卷积神经网络中真正占用内存的是什么

在卷积神经网络&#xff08;CNN&#xff09;中&#xff0c;占用内存的主要部分包括以下几个方面&#xff1a; 1. 模型参数&#xff08;Weights and Biases&#xff09; CNN 中的权重和偏置&#xff08;即模型的参数&#xff09;通常是占用内存的最大部分。具体来说&#xff1…

蓝桥杯每日真题 - 第7天

题目&#xff1a;&#xff08;爬山&#xff09; 题目描述&#xff08;X届 C&C B组X题&#xff09; 解题思路&#xff1a; 前缀和构造&#xff1a;为了高效地计算子数组的和&#xff0c;我们可以先构造前缀和数组 a&#xff0c;其中 a[i] 表示从第 1 个元素到第 i 个元素的…

automa 浏览器自动化工具插件

参考&#xff1a; https://github.com/AutomaApp/automa 安装后可以自己创建自动化工作流&#xff1a; 工具流插件可以选择

力扣 二叉树的直径-543

二叉树的直径-543 class Solution { int Max 0;//定义一个整形变量Max用来记录遍历二叉树中发现最大直径值 public:int diameterOfBinaryTree(TreeNode* root) {f(root);//定义一个用来求最大直径的函数return Max;}int f(TreeNode* root){//结束条件&#xff0c;如果访问到叶…

2024 年 Apifox 和 Postman 对比介绍详细版

Apifox VS Postman &#xff0c;当下流行的的两款 API 开发工具&#xff0c;2024 版对比&#xff01;

这10个SecureCRT小技巧,能让你用到65岁还不想退休!

SecureCRT是一款功能强大、高可用性的终端仿真器。在使用 SecureCRT 时&#xff0c;我发现了一些有用的技巧和窍门&#xff0c;总结如下。 窍门一&#xff1a;自动记录系统日志 配置网络或者系统设备&#xff0c;日志记录必可不少。 一方面记录设备的交互信息&#xff0c;方…