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