vue + swiper 实现图片轮播

1.安装swiper

    npm install swiper@3.4.1 --save-dev

2.在组件中引用 swiper

    import Swiper from 'swiper';
    import 'swiper/dist/css/swiper.min.css';

3.实例


<template>
  <div class="slide" v-on:mouseover="stop()" v-on:mouseout="move()">
    <div class="slideshow">
      <transition-group tag="ul" name="image">
        <li v-for="(img, index) in imgArray" v-show="index===mark" :key="index">
          <a href="#">
            <img :src='img'>
          </a>
        </li>
      </transition-group>
    </div>
    <div class="bar">
      <span v-for="(item, index) in imgArray" :class="{ 'active':index===mark }"
      @click="change(index)" :key="index"></span>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      timer: null, //定时器
      mark: 0, //比对图片索引的变量
      imgArray: [//图片路径
        require('../../assets/images/index/banner1.png'),
        require('../../assets/images/index/banner1.png'),
        require('../../assets/images/index/banner1.png'),
        require('../../assets/images/index/banner1.png')  
      ]
    }
  },
  methods: {
    autoPlay () {
      this.mark++;
      if (this.mark === 4) {
        this.mark = 0
      }
    },
    play () {
      this.timer = setInterval(this.autoPlay, 2500)
    },
    change (i) {
      this.mark = i
    },
    stop () {
      clearInterval(this.timer)
    },
    move () {
      this.timer = setInterval(this.autoPlay, 2500)
    }
  },
  created () {
    this.play()
  }
}
</script>


<style scoped>
  * {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .slide {
    width: 100%;
    height: 320px;
    margin: 0 auto;
    overflow: hidden;
    position: relative;
  }
  .slideshow {
    width: 100%;
    height: 320px;
  }
  .slideshow ul{
    width:100%;
    height: 320px;
  }
  li {
    width:100%;
    position: absolute;
  }
  .slideshow ul a{
    display: inline-block;
    width:100%;
  }
  img {
    width: 100%;
    height: 320px;
  }
  .bar {
    position: absolute;
    width: 100%;
    bottom: 10px;
    margin: 0 auto;
    z-index: 10;
    text-align: center;
  }
  .bar span {
    width: 10px;
    height: 10px;
    border-radius:50%;
    background: white;
    display: inline-block;
    margin-right: 10px;
  }
  .active {
    background: red !important;
  }
  .image-enter-active {
    transform: translateX(0);
    transition: all 1.5s ease;
  }
  .image-leave-active {
    transform: translateX(-100%);
    transition: all 1.5s ease;
  }
  .image-enter {
    transform: translateX(100%);
  }
  .image-leave {
    transform: translateX(0);
  }

</style>


Vue.js 结合 Swiper 是一种常见的组合,用于创建动态且响应式的轮播图组件。Swiper 是一款流行的前端轮播库,它提供了丰富的选项和易用的 API,而 Vue 可以帮助我们管理数据绑定、状态管理和组件复用。 以下是使用 VueSwiper 实现叠层轮播图的基本步骤: 1. **安装 Swiper**: 首先,你需要在项目中引入 Swiper 的库。可以使用 npm 或 yarn 进行安装: ```bash npm install swiper@latest swiper-auto-init --save # 或者 yarn add swiper@latest swiper-auto-init ``` 2. **导入 Swiper 组件**: 在你的 Vue 文件中,导入 Swiper 的 CSS 和 JavaScript,并使用 `v-bind` 将其属性绑定到组件上: ```html <link rel="stylesheet" href="path/to/swiper.min.css"> <script src="path/to/swiper-bundle.min.js"></script> ``` ```js import { Swiper, SwiperSlide } from 'swiper'; ``` 3. **创建模板和数据**: 创建一个包含 Swiper 组件的 Vue 模板,并定义数据数组,存放轮播图片及其内容: ```html <div id="mySwiper"> <swiper :options="swiperOptions"> <swiper-slide v-for="(item, index) in images" :key="index"> <img :src="item.image" alt="Slide {{ index + 1 }}"> <!-- 添加额外内容如标题等 --> <div class="slide-content">{{ item.title }}</div> </swiper-slide> </swiper> </div> ``` ```js data() { return { images: [ // 图片数组,例如 [{'image': 'img1.jpg', 'title': '图1'}, ...] ], swiperOptions: { // Swiper 的配置选项,如 autoplay、pagination 等 }, }; }, ``` 4. **初始化 Swiper**: 在 Vue 的 mounted 生命周期钩子函数里初始化 Swiper 组件,传入数据配置: ```js mounted() { this.$refs.mySwiper.swiper.init(); } ``` 5. **调整样式**:根据需要调整 Swiper 的样式和布局,比如添加 pagination 控制点、改变切换效果等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值