轮播 Carousel
轮播组件,用于滚动显示图片等
基础用法
直接用组件默认插槽插入即可
<template>
<b-carousel loop trigger="click">
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
<template>
<b-carousel loop trigger="click">
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
自动切换和速度
可以设置autoplay
自动切换,trigger="hover"
悬停切换
<template>
<b-carousel autoplay :interval="2000" :initial-index="value" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const value = ref(1)
</script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
<template>
<b-carousel autoplay :interval="2000" :initial-index="value" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const value = ref(1)
</script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
指示器样式和位置
<template>
<div>
<b-carousel indicator-position="inside" arrow="always" trigger="hover" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
<b-divider></b-divider>
<b-carousel indicator-position="outside" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</div>
</template>
<script setup lang="ts"></script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
<template>
<div>
<b-carousel indicator-position="inside" arrow="always" trigger="hover" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
<b-divider></b-divider>
<b-carousel indicator-position="outside" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</div>
</template>
<script setup lang="ts"></script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
卡片风格
可以设置card
卡片风格
<template>
<b-carousel :interval="4000" type="card" height="200px" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<script setup lang="ts"></script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
<template>
<b-carousel :interval="4000" type="card" height="200px" loop>
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<script setup lang="ts"></script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
方向
可以设置direction
方向为竖直方向
<template>
<b-carousel height="200px" direction="vertical" :autoplay="false">
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<script setup lang="ts"></script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
<template>
<b-carousel height="200px" direction="vertical" :autoplay="false">
<b-carousel-item>
<div class="demo-carousel-panel">1</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">2</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">3</div>
</b-carousel-item>
<b-carousel-item>
<div class="demo-carousel-panel">4</div>
</b-carousel-item>
</b-carousel>
</template>
<script setup lang="ts"></script>
<style scoped>
.demo-carousel-panel {
height: 200px;
line-height: 200px;
text-align: center;
color: #fff;
font-size: 20px;
background: #99a9bf;
}
</style>
Props
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
initial-index | 轮播的索引,从 0 开始, | Number | — | 0 |
height | 走马灯的高度,可填 auto 或具体高度数值,单位 px | String / Number | — | 200px |
loop | 是否开启循环 | Boolean | — | false |
autoplay | 是否自动切换 | Boolean | — | false |
interval | 自动切换的时间间隔,单位为毫秒 | Number | — | 3000 |
indicator-position | 指示器的位置 | String | inside (内部),outside(外部),none(不显示) | inside |
trigger | 触发方式 | String | click,hover | click |
arrow | 切换箭头的显示时机 | String | hover,always,never | hover |
direction | 方向 | String | horizontal、vertical | horizontal |
type | 类型 | String | card | — |
Events
事件名 | 说明 | 返回值 |
---|---|---|
change | 轮播切换时触发,目前激活的索引,原的索引 | oldValue, value |