• 在Vue中使用Swiper轮播图、同时解决点击轮播图左右切换按钮不生效的问题、同时将轮播图抽离出为一个公共组件


    轮播图左右的切换按钮、如果点击没有反应,控制台也没有报错。很大可能是版本问题。如果不指定版本信息、默认安装的是最新的版本。版本过高或者过低都有可能导致无效。目前兼容性和稳定性比较好的是:5.4.5

    官网地址:https://www.swiper.com.cn/

    效果展示

    Vue中使用轮播图

    1、安装Swiper

    npm i swiper@5.4.5
    
    • 1

    在这里插入图片描述
    在这里插入图片描述

    2、在要使用的页面引入swiper

    注:也可以在全局引入、这样在其它页面都可以使用到了。我这里就一个页面使用、就单独在某一个页面引入了。

    import Swiper from "swiper";
    import "swiper/css/swiper.min.css";
    
    • 1
    • 2

    在这里插入图片描述

    3、轮播图的位置

    3.1 设置一个div放置到页面对应位置

    分页器、切换按钮的颜色大小、以及切换效果都可以进行设置。

    swiper学习网址

     <div class="swiper-container">
              <div class="swiper-wrapper">
                <div class="swiper-slide" v-for="(item, i) in images" :key="i">
                  <img class="carousel-img" :src="item.img" alt="" />
                div>
              div>
              
              <div class="swiper-pagination">div>
              
              <div class="swiper-button-prev">div>
              <div class="swiper-button-next">div>
            div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.2 设置轮播图的大小和图片完全填充

    .swiper-container {
      height: 350px;
      width: 95%;
    }
    
    .carousel-img {
      width: 100%;
      height: 100%;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.3 轮播图片

    这里使用双向数据绑定、这里的轮播图片后期可以进行替换。比如从后端接口返回的轮播图片替换数组中的。这里暂时写死

        images: [
            { img: "https://www.baidu.com/img/baidu_jgylogo3.gif" },
            { img: "http://localhost:8282/images/21667218837206.jpg" },
            { img: "http://localhost:8282/images/21667218837206.jpg" },
          ],
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.4 初始化一个轮播图

      mounted() {
        var mySwiper = new Swiper(".swiper-container", {
          autoplay: {
            delay: 5000,
            disableOnInteraction: false,
          }, //可选选项,自动滑动
    
          loop: true, // 循环模式选项
          speed: 4000,
    
          // 如果需要分页器
          pagination: {
            el: ".swiper-pagination",
            clickable: true,
          },
    
          // 如果需要前进后退按钮
          navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
            disabledClass: "my-button-disabled",
          },
        });
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4、自己遇到的问题

    4.1 版本不正确

    开始使用的3.4.2 版本的、使用这个版本导致轮播图的左右切换按钮不好使
    在这里插入图片描述

    4.2 如何在项目中卸载已经安装的包?

    npm uninstall swiper
    
    • 1

    5、如何将Swiper抽离成一个组件?

    这里不难看出、全部写在一个页面、会导致页面很臃肿。不如直接将swiper抽离成一个公共组件。哪个页面想使用,直接引入组件

    5.1 抽离出公共组件

    MySwiper.vue

    <template>
      <div class="swiper-container">
        <div class="swiper-wrapper">
          <div class="swiper-slide" v-for="(item, i) in images" :key="i">
            <img class="carousel-img" :src="item.img" alt="" />
          div>
        div>
        
        <div class="swiper-pagination">div>
        
        <div class="swiper-button-prev">div>
        <div class="swiper-button-next">div>
      div>
    template>
    
    <script>
    import Swiper from "swiper";
    import "swiper/css/swiper.min.css";
    export default {
      name: "MySwiper",
      data() {
        return {
          images: [
            { img: "https://www.baidu.com/img/baidu_jgylogo3.gif" },
            { img: "http://localhost:8282/images/21667218837206.jpg" },
            { img: "http://localhost:8282/images/21667218837206.jpg" },
          ],
        };
      },
      mounted() {
        var mySwiper = new Swiper(".swiper-container", {
          autoplay: {
            delay: 5000,
            disableOnInteraction: false,
          }, //可选选项,自动滑动
    
          loop: true, // 循环模式选项
          speed: 4000,
    
          // 如果需要分页器
          pagination: {
            el: ".swiper-pagination",
            clickable: true,
          },
    
          // 如果需要前进后退按钮
          navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
            disabledClass: "my-button-disabled",
          },
        });
      },
    };
    script>
    
    
    <style scoped>
    .carousel-img {
      width: 100%;
      height: 100%;
    }
    
    .swiper-container {
      height: 350px;
      width: 95%;
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    5.2 在对应页面引入

    import MySwiper from "@/components/MySwiper";
    
      components: {
        MySwiper
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    5.3 将组件放到对应位置

    组件中可以进行传参的、具体怎样传参。我之前笔记有些、同样可以将后端返回的轮播图图片替换掉数组中的图片。这样就可以动态改变轮播图的图片

        
            <MySwiper>MySwiper>
    
    • 1
    • 2

    在这里插入图片描述

    6、后语

    进一步加深了对组件的使用、轮播图好用。学无止境。。。。。。

  • 相关阅读:
    【操作系统】操作系统的概述
    【漏洞复现】E-office文件包含漏洞
    zookeeper 3.8.1安装和入门使用
    mysql redo 日志 、 undo 日志 、binlog
    Mongodb字段更新操作符$currentDate
    基于JAVA网上书店的设计与实现计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    制作一个简单HTML大学生抗疫感动专题网页(HTML+CSS)
    【手写数据库toadb】代码又更新了,增加了解析树,查询树,执行计划,向更多复杂SQL迈进了一步
    Rust 添加老版本的依赖时报错
    百度地图数据可视化
  • 原文地址:https://blog.csdn.net/weixin_43304253/article/details/127627704