一、介绍
Vue走马灯组件(Carousel)是一种流行的前端UI组件,其可用于展示一系列图片或者一系列HTML片段。这种组件多用于网站首页或者商城等展示性页面,是一种常用的网页交互效果。下面我们就来详细介绍一下Vue走马灯实现轮播图案例。
二、实现
1. 安装Vue和Vue-Carousel
npm install vue --save npm install vue-carousel --save
2. 引入Vue-Carousel组件
在需要使用Vue-Carousel的组件中引入:
<template>
  <div id="carousel-example">
    <carousel :perPageCustom="[[320, 1], [480, 2], [720, 3], [960, 4], [1200, 5]]" :autoplay="true">
      <slide v-for="(slide,index) in slides" :key="index">
        <div>
          <img :src="slide.image" :alt="slide.title" />
          <h3>{{slide.title}}</h3>
          <p>{{slide.description}}</p>
        </div>
      </slide>
    </carousel>
  </div>
</template>
<script>
  import VueCarousel from 'vue-carousel';
  export default {
    name: 'CarouselExample',
    components: {Carousel, Slide},
    data: () => ({
      slides: [
        {
          title: 'Slide 1',
          description: 'This is a description for slide 1',
          image: 'https://picsum.photos/id/58/600/400',
        },
        {
          title: 'Slide 2',
          description: 'This is a description for slide 2',
          image: 'https://picsum.photos/id/237/600/400',
        },
        {
          title: 'Slide 3',
          description: 'This is a description for slide 3',
          image: 'https://picsum.photos/id/87/600/400',
        },
        {
          title: 'Slide 4',
          description: 'This is a description for slide 4',
          image: 'https://picsum.photos/id/130/600/400',
        },
        {
          title: 'Slide 5',
          description: 'This is a description for slide 5',
          image: 'https://picsum.photos/id/236/600/400',
        },
      ],
    }),
  };
</script>
3. 属性说明
- perPageCustom:一个包含断点和滑块数的原始数组。)
- autoplay:表示是否自动播放,设置为true可以自动播放,默认为false。
- slide:用于包裹所有轮播图列表项目的容器元素。
- arrow:指示器左右箭头容器元素,如果不传入,则不显示箭头。
- indicator:指示器容器元素,控制轮播图当前页码显示情况。
- per-page:定义每行显示滑块的个数。
- touch:是否允许线上手机浏览器和触屏设备上使用触摸拖动。
- autoplay-timeout:定义轮播图自动轮播的间隔,以毫秒为单位。
- height:定义轮播图的高度。
4. 样式说明
定义针对Vue-Carousel的基本样式,样式代码如下:
.carousel {
  position: relative;
  .arrow {
    position: absolute;
    top: 40%;
    z-index: 11;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .arrow-left {
    left: 15px;
  }
  .arrow-right {
    right: 15px;
  }
  .indicator {
    position: absolute;
    bottom: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
    span {
      width: 10px;
      height: 10px;
      border-radius: 50%;
      background-color: gray;
      margin: 5px;
      cursor: pointer;
      &.active {
        background-color: #f00;
      }
    }
  }
}
总结
以上就是Vue走马灯组件(Carousel)实现轮播图的简介。Vue-Carousel是使用Vue.js编写的轻量级走马灯组件,能够快捷地实现页面中轮播图的效果。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/240477.html
 
 微信扫一扫
微信扫一扫  支付宝扫一扫
支付宝扫一扫 