044_Carousel走马灯

本文详细介绍了Element UI中的Carousel组件,包括其属性、事件、方法及示例。Carousel走马灯可用于循环播放图片、文字等内容,支持设置高度、初始索引、触发方式、自动切换、指示器位置、切换箭头显示时机等多种功能。此外,还展示了如何通过Vue.js创建不同类型的走马灯实例,如基础用法、指示器位置、切换箭头显示和卡片模式等,以及调整方向为垂直展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Carousel走马灯

1.1. Carousel走马灯在有限空间内, 循环播放同一类型的图片、文字等内容。

1.2. Carousel Attributes

参数

说明

类型

可选值

默认值

height

走马灯的高度

string

initial-index

初始状态激活的幻灯片的索引, 从0开始

number

0

trigger

指示器的触发方式

string

click

autoplay

是否自动切换

boolean

true

interval

自动切换的时间间隔, 单位为毫秒

number

3000

indicator-position

指示器的位置

string

outside/none

arrow

切换箭头的显示时机

string

always/hover/never

hover

type

走马灯的类型

string

card

loop

是否循环显示

boolean

true

direction

走马灯展示的方向

string

horizontal/vertical

horizontal

1.3. Carousel Events

事件名称

说明

回调参数

change

幻灯片切换时触发

目前激活的幻灯片的索引, 原幻灯片的索引

1.4. Carousel Methods

方法名

说明

参数

setActiveItem

手动切换幻灯片

需要切换的幻灯片的索引, 从0开始; 或相应el-carousel-item的name属性值

prev

切换至上一张幻灯片

next

切换至下一张幻灯片

1.5. Carousel-Item Attributes

参数

说明

类型

可选值

默认值

name

幻灯片的名字, 可用作setActiveItem的参数

string

label

该幻灯片所对应指示器的文本

string

2. Carousel走马灯例子

2.1. 使用脚手架新建一个名为element-ui-carousel的前端项目, 同时安装Element插件。

2.2. 编辑index.js 

import Vue from 'vue'
import VueRouter from 'vue-router'
import Carousel from '../components/Carousel.vue'
import OutsideCarousel from '../components/OutsideCarousel.vue'
import AlwaysArrowCarousel from '../components/AlwaysArrowCarousel.vue'
import CardCarousel from '../components/CardCarousel.vue'
import VerticalCarousel from '../components/VerticalCarousel.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/Carousel' },
  { path: '/Carousel', component: Carousel },
  { path: '/OutsideCarousel', component: OutsideCarousel },
  { path: '/AlwaysArrowCarousel', component: AlwaysArrowCarousel },
  { path: '/CardCarousel', component: CardCarousel },
  { path: '/VerticalCarousel', component: VerticalCarousel }
]

const router = new VueRouter({
  routes
})

export default router

2.3. 在components下创建Carousel.vue

<template>
  <div>
    <h1>基础用法</h1>
    <h4>结合使用el-carousel和el-carousel-item标签就得到了一个走马灯。幻灯片的内容是任意的, 需要放在el-carousel-item标签中。默认情况下, 在鼠标hover底部的指示器时就会触发切换。通过设置trigger属性为click, 可以达到点击触发的效果。</h4>
    <div>
      <span>默认Hover指示器触发</span>
      <el-carousel height="150px">
        <el-carousel-item v-for="item in 4" :key="item">
          <h3 class="small">{{ item }}</h3>
        </el-carousel-item>
      </el-carousel>
    </div>
    <div>
      <span>Click指示器触发</span>
      <el-carousel trigger="click" height="150px">
        <el-carousel-item v-for="item in 4" :key="item">
          <h3 class="small">{{ item }}</h3>
        </el-carousel-item>
      </el-carousel>
    </div>
  </div>
</template>

<style scoped>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }
  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }
  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
</style>

2.4. 在components下创建OutsideCarousel.vue

<template>
  <div>
    <h1>指示器</h1>
    <h4>indicator-position属性定义了指示器的位置。默认情况下, 它会显示在走马灯内部, 设置为outside则会显示在外部; 设置为none则不会显示指示器。</h4>
    <el-carousel indicator-position="outside">
      <el-carousel-item v-for="item in 4" :key="item">
        <h3>{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<style scoped>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>

2.5. 在components下创建AlwaysArrowCarousel.vue

<template>
  <div>
    <h1>切换箭头</h1>
    <h4>arrow属性定义了切换箭头的显示时机。默认情况下, 切换箭头只有在鼠标hover到走马灯上时才会显示; 若将arrow设置为always, 则会一直显示; 设置为never, 则会一直隐藏。</h4>
    <el-carousel :interval="5000" arrow="always">
      <el-carousel-item v-for="item in 4" :key="item">
        <h3>{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<style  scoped>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>

2.6. 在components下创建CardCarousel.vue

<template>
  <div>
    <h1>卡片化</h1>
    <h4>将type属性设置为card即可启用卡片模式。从交互上来说, 卡片模式和一般模式的最大区别在于, 可以通过直接点击两侧的幻灯片进行切换。</h4>
     <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item in 6" :key="item">
        <h3 class="medium">{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<style scoped>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
  }
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>

2.7. 在components下创建VerticalCarousel.vue

<template>
  <div>
    <h1>方向</h1>
    <h4>默认情况下, direction为horizontal。通过设置direction为vertical来让走马灯在垂直方向上显示。</h4>
    <el-carousel height="200px" direction="vertical" :autoplay="false">
      <el-carousel-item v-for="item in 3" :key="item">
        <h3 class="medium">{{ item }}</h3>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<style scoped>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
  }
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>

2.8. 运行项目, 访问http://localhost:8080/#/Carousel

2.9. 运行项目, 访问http://localhost:8080/#/OutsideCarousel

2.10. 运行项目, 访问http://localhost:8080/#/AlwaysArrowCarousel

2.11. 运行项目, 访问http://localhost:8080/#/CardCarousel

2.12. 运行项目, 访问http://localhost:8080/#/VerticalCarousel

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值