图片轮播-vue

251 阅读1分钟

做一个中间大两头小的轮播图,使用vue插件 html代码

<template>
  <div class="img-change">
    <ul>
      <li v-for="(item, index) of img" :key="index">
        <img class="pic" :src="require(`../assets/images/img-${item}.png`)" />
      </li>
    </ul>
    <img class="left arrow" @click="replace(false)" src="../assets/images/left.png" alt />
    <img class="right arrow" @click="replace(true)" src="../assets/images/right.png" alt />
  </div>
</template>

js代码

  data() {
    return {
      img: [1, 2, 3, 4, 5],
    };
  },
    created() {
    // 设置定时器;
    setInterval(() => {
      this.replace(true);
    }, 3000);
  },
      replace(right) {
      let temp;
      if (right) {
        temp = this.img[this.img.length - 1];
        for (let i = this.img.length - 1; i > 0; i--) {
          this.$set(this.img, i, this.img[i - 1]);
        }
        this.$set(this.img, 0, temp);
      } else {
        temp = this.img[0];
        for (let i = 0; i < this.img.length - 1; i++) {
          this.$set(this.img, i, this.img[i + 1]);
        }
        this.$set(this.img, this.img.length - 1, temp);
      }
    }

css 代码

.img-change {
  position: absolute;
  top: 2436px;
  width: 560px;
  // width: 200%;
  height: 583px;
  left: 50%;
  transform: translate(-50%);
  ul {
    padding: 0;
    margin: 0;
    list-style: none;
    li {
      position: absolute;
      float: left;
      img {
        width: 350px;
        position: absolute;
        left: 100px;
      }
      &:nth-child(1) {
        img {
          position: absolute;
          left: -20px;
          top: 40px;
          width: 300px;
          height: 455px;
        }
      }
      &:nth-child(2) {
        img {
          z-index: 2;
        }
      }
      &:nth-child(3) {
        img {
          position: absolute;
          left: 280px;
          top: 40px;
          width: 300px;
          height: 455px;
        }
      }
    }
  }
  .arrow {
    position: absolute;
    top: 50%;
    transform: translate(-50%);
    width: 55px;
  }
  .left {
    left: -55px;
  }
  .right {
    right: -115px;
  }
}

最终效果图