vue2+swiper——实现多图轮播+层叠轮播——技能提升

今天看到同事在写轮播图,由于是jq的写法,我没有过多参与,我只写vue的部分。。。虽然语言不一样,但是用法还是要会的。下面介绍通过swiper组件来实现轮播效果。

解决步骤1:安装swiper

npm install swiper@5.4.5
我这边安装的版本是5.4.5

解决步骤2:在指定页面中局部注册

import Swiper from 'swiper'; // 注意引入的是Swiper
import 'swiper/css/swiper.min.css'; // 注意这里的引入

解决步骤3:多图轮播效果

在这里插入图片描述
参考链接:https://blog.csdn.net/qq_51055690/article/details/132402936

3.1html代码

 <div class="banshi">
      <div class="swiper-container">
        <div class="swiper-wrapper">
          <div
            v-for="item in swiperList"
            :key="item.id"
            class="swiper-slide"
            :style="`background-image:url(${item.imgUrl})`"
          >
            <h3>{{ item.title }}</h3>
            <el-button>立即办理</el-button>
          </div>
        </div>
        <div class="swiper-pagination"></div>
        <div class="swiper-button-next"></div>
        <div class="swiper-button-prev"></div>
      </div>
    </div>

3.2 js代码

swiper: null,
swiperList: [
 {
   id: 1,
   title: '采集国家二级保护野生植物审批',
   imgUrl:
     'https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500',
 },
 {
   id: 2,
   title: '农村危房改造',
   imgUrl:
     'https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500',
 },
 {
   id: 3,
   title: '农村危房改造',
   imgUrl:
     'https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500',
 },
 {
   id: 4,
   title: '农村危房改造',
   imgUrl:
     'https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500',
 },
],

3.3 methods方法

getSwiper() {
   this.swiper = new Swiper('.swiper-container', {
     loop: true, // 无缝
        autoplay: { //自动开始
            delay: 3000, //时间间隔
            disableOnInteraction: false, //*手动操作轮播图后不会暂停*
        },
        paginationClickable: true,
        slidesPerView: 4, // 一组三个
        spaceBetween: 30, // 间隔
        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        // 窗口变化,重新init,针对F11全屏和放大缩小,必须加
        observer: true,
        observeParents: true,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
            clickable: true, // 分页器可以点击
        }, 
    });
 },

此方法要在mounted中使用:

mounted() {
  this.getSwiper();
},

3.4 css样式

<style lang="less">
.banshi {
  width: 1200px;
  margin: 0 auto;
  padding: 70px 0;
  .swiper-slide {
    position: relative;
    height: 328px;
    width: 264px;
    padding: 36px 22px;
    background-repeat: no-repeat;
    background-size: contain;
    background-color: #f7f8fa;
    .el-button {
      z-index: 2;
    }
  }
}
</style>

解决步骤4:层叠轮播效果

在这里插入图片描述
参考链接:https://www.cnblogs.com/a973692898/p/13364563.html

4.1 html代码

<div class="ours-main">
      <div class="ours-swiper">
        <div class="swiper-container">
          <div class="swiper-wrapper">
            <div class="swiper-slide">
              <div class="s-left">
                <div class="s-l-block">
                  <img
                    src="https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500"
                  />
                </div>
              </div>
              <div class="s-right">
                <div class="s-r-title">
                  <span>外教abcd</span>
                </div>
              </div>
            </div>
            <div class="swiper-slide">
              <div class="s-left">
                <div class="s-l-block">
                  <img
                    src="https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500"
                  />
                </div>
              </div>
              <div class="s-right">
                <div class="s-r-title">
                  <span>外教abcd</span>
                </div>
              </div>
            </div>
            <div class="swiper-slide">
              <div class="s-left">
                <div class="s-l-block">
                  <img
                    src="https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500"
                  />
                </div>
                 
              </div>
              <div class="s-right">
                <div class="s-r-title">
                  <span>外教abcd</span>
                </div>
              </div>
            </div>
            <div class="swiper-slide">
              <div class="s-left">
                <div class="s-l-block">
                  <img
                    src="https://img0.baidu.com/it/u=2639758887,1655012884&fm=253&fmt=auto&app=138&f=PNG?w=632&h=500"
                  />
                </div>
              </div>
              <div class="s-right">
                <div class="s-r-title">
                  <span>外教abcd</span>
                </div>
              </div>
            </div>
          </div>
          <div class="swiper-pagination"></div>
          <!-- Add Arrows -->
          <div class="swiper-button-next swiper-button-black"></div>
          <div class="swiper-button-prev swiper-button-black"></div>
        </div>
      </div>
    </div>

4.2 methods方法

getSwiper() {
  var swiper = new Swiper('.swiper-container', {
    slidesPerView: 1.5, //设置slider容器能够同时显示的slides数量(carousel模式)。
    spaceBetween: -180, //在slide之间设置距离(单位px)。
    centeredSlides: true, //设置活动块居中
    loop: true,
    pagination: {
      el: '.swiper-pagination',
      clickable: true,
    },
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
    autoplay: {
      stopOnLastSlide: true,
      delay: 5000,
    },
  });
},

此方法要在mounted中使用:

mounted() {
  this.getSwiper();
},

4.3 css样式

<style scoped lang="less">
/*展示区域*/
.ours-main {
  width: 100%;
  height: 28rem;
  margin-top: 3rem;
  display: flex;
  justify-content: center;
}
.ours-swiper {
  height: 100%;
  width: 49.8%;
}
/*自定义swiper样式*/
.swiper-container {
  position: relative;
  width: 100%;
  height: 100%;
}
.swiper-wrapper {
  width: 100%;
  height: 100%;
}
.swiper-slide {
  text-align: center;
  height: 86%;
  background: #cccdce;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: 300ms;
  transform: scale(0.8);
}

.swiper-slide-active,
.swiper-slide-duplicate-active {
  transform: scale(1);
}
.swiper-button-next,
.swiper-button-prev {
  display: block;
}
.swiper-slide-active {
  z-index: 999 !important;
}
.swiper-slide-prev .s-right {
  display: none;
}
.swiper-slide-prev .s-left {
  margin-left: 35%;
}
.swiper-slide-prev {
  background-color: #fff;
}
.swiper-slide-next {
  background-color: #fff;
}
/*要展示的div左侧样式*/
.s-left {
  width: 50%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.s-l-block {
  width: 92.2%;
  height: 85%;
}
.s-l-block img {
  width: 100%;
  height: 100%;
}
/*要展示div的右侧样式*/
.s-right {
  width: 45%;
  height: 100%;
}
.s-r-title {
  margin-top: 2rem;
  margin-right: 10%;
  font-size: 2rem;
  color: #676769;
  font-weight: 500;
}
</style>

相关推荐

  1. Swiper

    2024-05-09 11:34:03       39 阅读
  2. Swiper后端接口实现

    2024-05-09 11:34:03       46 阅读
  3. Flutter 使用flutter_swiper_null_safety 实现

    2024-05-09 11:34:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-09 11:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-09 11:34:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-09 11:34:03       82 阅读
  4. Python语言-面向对象

    2024-05-09 11:34:03       91 阅读

热门阅读

  1. 浏览器原理---浏览器同源策略

    2024-05-09 11:34:03       32 阅读
  2. 【Web前端】定位_浮动_音视频

    2024-05-09 11:34:03       31 阅读
  3. Python进阶之-hashlib详解

    2024-05-09 11:34:03       30 阅读
  4. CMakeLists.txt语法规则:条件判断中表达式说明四

    2024-05-09 11:34:03       31 阅读
  5. React 学习-2

    2024-05-09 11:34:03       32 阅读
  6. React 第二十五章 React.memo

    2024-05-09 11:34:03       31 阅读
  7. MY SQL 实验四:

    2024-05-09 11:34:03       27 阅读
  8. LeetCode 每日一题 ---- 【2079.给植物浇水】

    2024-05-09 11:34:03       29 阅读
  9. 数据平台—邮件服务

    2024-05-09 11:34:03       38 阅读
  10. Linux源码包安装

    2024-05-09 11:34:03       28 阅读
  11. FFmpeg的GPU功能进行视频处理

    2024-05-09 11:34:03       30 阅读