Vue 实现轮播图

<template>
  <div class="carousel">
    <div class="main" ref="main">
      <!--图片盒子-->
      <div class="banner">
        <a href="javascript:" v-for=" (item, index) in images" :key="index">
          <!--设置div背景图片实现插入图片效果,而不是img标签,提高浏览器性能-->
          <div
            class="banner-slide"
            :style="{ backgroundImage: `url(${item})`}"
            @mouseover="mouseOver()"
            @mouseout="mouseOut(index)"
            ref="slide_img"
          ></div>
        </a>
      </div>
      <!--切换按钮-->
      <a href="javascript:void(0)" class="button prev" ref="prev" @click="toPrev"></a>
      <a href="javascript:void(0)" class="button next" ref="next" @click="toNext"></a>
      <div class="dots">
        <span
          v-for=" (item, index) in dots"
          :key="index"
          @click="dotsChecked(index)"
          ref="dots"
          :class="index === 0 ? { dots_active } : '' "
        ></span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "carousel",
  data() {
    return {
      index: 0,
      timer: null,
      images: [
        "https://i1.mifile.cn/a4/xmad_1562317906034_yYDfN.jpg",
        "https://i1.mifile.cn/a4/xmad_15625789613185_yOkET.jpg",
        "https://i1.mifile.cn/a4/xmad_15623260229891_iSOkA.jpg"
      ],
      dots: [0, 1, 2],
      dots_active: true
    };
  },
  created() {
    this.mouseOut(this.index);
  },
  methods: {
    changeImg(index) {
      for (let i = 0; i < this.images.length; i++) {
        this.$refs.slide_img[i].style.display = "none";
        this.$refs.dots[i].className = " ";
      }

      this.$refs.slide_img[index].style.display = "block";
      this.$refs.dots[index].className = "dots_active";
    },
    mouseOver() {
      if (this.timer) clearInterval(this.timer);
    },
    mouseOut(index) {
      let that = this;
      this.timer = setInterval(function() {
        index++;
        if (index >= that.images.length) {
          index = 0;
        }
        that.index = index;
        that.changeImg(index);
      }, 2000);
    },
    toPrev() {
      this.index--;
      if (this.index < 0) this.index = this.images.length - 1;
      this.changeImg(this.index);
    },
    toNext() {
      this.index++;
      if (this.index >= this.images.length) this.index = 0;
      this.changeImg(this.index);
    },
    dotsChecked(index) {
      for (let i = 0; i < this.images.length; i++) {
        this.$refs.dots[i].className = " ";
      }
      this.changeImg(index);
    }
  }
};

</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}
.main {
  width: 1200px;
  height: 460px;
  margin: 30px auto;
  overflow: hidden;
  position: relative;
}
.banner {
  width: 1200px;
  height: 460px;
  position: relative;
}
.banner-slide {
  width: 1226px;
  height: 460px;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 100% 100%;
  position: absolute;
}
.button {
  width: 40px;
  height: 80px;
  position: absolute;
  left: 244px;
  top: 50%;
  margin-top: -40px;
  background: url("../assets/left_button.png") no-repeat center center;
  &:hover {
    background-color: #dcdcdc;
    opacity: 0.8;
  }
}
.next {
  left: auto;
  right: 0;
  transform: rotate(180deg);
}
.dots {
  position: absolute;
  right: 20px;
  bottom: 10px;
  span {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background-color: rgba(7, 17, 27, 0.4);
    box-shadow: 0 0 4px rgba(255, 255, 255, 0.8) inset;
    margin-left: 10px;
    cursor: pointer;

  }
}
.dots_active {
  box-shadow: 0 0 4px rgba(7, 17, 27, 0.4) inset !important;
  background-color: #ffffff !important;
 
}
</style>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值