vue轮播图组件,自动播放,小圆点或按钮进行切换

默认自动播放,进入轮播图片或按钮取消播放,按钮以及圆点进行切换图片

<style lang="scss" scoped>
.banner {
  width: 260px;
  margin-left: 20px;
  overflow: hidden;
  border: 1px solid red;

  .list {
    width: 2000%;
    margin-top: 28px;
    text-align: left;
    overflow: hidden;
    position: relative;
    .item {
      display: inline-block;
      vertical-align: middle;
      width: 230px;
      height: 100px;
      padding-right: 30px;

      .item-img {
        width: 100%;
        height: 100%;
        cursor: pointer;
        border: 1px solid #ccc;

        .img {
          display: block;
          width: 100%;
          height: 100%;
          border: 1px solid #ccc;
        }
      }
    }
  }

  .warp-dots{
    text-align: center;
    .dots-item{
      display: inline-block;
      width: 5px;
      height: 5px;
      margin: 0 2px;
      background-color: #000;
      border-radius: 50%;
      cursor: pointer;
      &.active{
       background-color: #19b9e7;
      }
    }
  }
}
</style>
<template>
  <div
    class="banner"
    ref="bannerRef"
    @mouseenter="bannerEnter"
    @mouseleave="bannerLeave">
    <ul class="list" ref="listRef" :style="listStyle">
      
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue轮播图组件封装是一种常见的前端开发技术,它可以实现在网页中展示多张图片,并通过组件间的数据通信来实现图片切换等功能。以下是一个简单的Vue轮播图组件封装的示例: 1. 创建一个Vue组件,命为Carousel: ```javascript <template> < class="carousel"> <img :src="currentImage" alt="carousel image" /> <button @click="prev">Previous</button> <button @click="next">Next</button> </div> </template> <script> export default { data() { return { images: [ "image1.jpg", "image2.jpg", "image3.jpg" ], currentIndex: 0 }; }, computed: { currentImage() { return this.images[this.currentIndex]; } }, methods: { prev() { this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length; }, next() { this.currentIndex = (this.currentIndex + 1) % this.images.length; } } }; </script> <style scoped> .carousel { width: 100%; height: 300px; position: relative; } .carousel img { width: 100%; height: 100%; object-fit: cover; } .carousel button { position: absolute; top: 50%; transform: translateY(-50%); } </style> ``` 2. 在父组件中使用Carousel组件,并通过props属性传递数据: ```javascript <template> <div> <carousel :images="carouselImages"></carousel> </div> </template> <script> import Carousel from "@/components/Carousel.vue"; export default { data() { return { carouselImages: [ "image1.jpg", "image2.jpg", "image3.jpg" ] }; }, components: { Carousel } }; </script> ``` 在上述示例中,Carousel组件通过props属性接收父组件传递的images数据,并在computed属性中根据currentIndex动态计算当前显示的图片路径。通过点击按钮触发prev和next方法来切换图片。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值