ElementPlus Carousel 走馬燈

2021-09-27 09:41 更新

Carousel 走馬燈

在有限空間內(nèi),循環(huán)播放同一類型的圖片、文字等內(nèi)容

基礎(chǔ)用法

適用廣泛的基礎(chǔ)用法


結(jié)合使用el-carousel和el-carousel-item標(biāo)簽就得到了一個走馬燈。幻燈片的內(nèi)容是任意的,需要放在el-carousel-item標(biāo)簽中。默認(rèn)情況下,在鼠標(biāo) hover 底部的指示器時就會觸發(fā)切換。通過設(shè)置trigger屬性為click,可以達到點擊觸發(fā)的效果。

<template>
  <div class="block">
    <span class="demonstration">默認(rèn) Hover 指示器觸發(fā)</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 class="block">
    <span class="demonstration">Click 指示器觸發(fā)</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>
</template>

<style>
  .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>

指示器

可以將指示器的顯示位置設(shè)置在容器外部


indicator-position屬性定義了指示器的位置。默認(rèn)情況下,它會顯示在走馬燈內(nèi)部,設(shè)置為outside則會顯示在外部;設(shè)置為none則不會顯示指示器。

<template>
  <el-carousel indicator-position="outside">
    <el-carousel-item v-for="item in 4" :key="item">
      <h3>{{ item }}</h3>
    </el-carousel-item>
  </el-carousel>
</template>

<style>
  .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>

切換箭頭

可以設(shè)置切換箭頭的顯示時機


arrow屬性定義了切換箭頭的顯示時機。默認(rèn)情況下,切換箭頭只有在鼠標(biāo) hover 到走馬燈上時才會顯示;若將arrow設(shè)置為always,則會一直顯示;設(shè)置為never,則會一直隱藏。

<template>
  <el-carousel :interval="5000" arrow="always">
    <el-carousel-item v-for="item in 4" :key="item">
      <h3>{{ item }}</h3>
    </el-carousel-item>
  </el-carousel>
</template>

<style>
  .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>

卡片化

當(dāng)頁面寬度方向空間空余,但高度方向空間匱乏時,可使用卡片風(fēng)格


將type屬性設(shè)置為card即可啟用卡片模式。從交互上來說,卡片模式和一般模式的最大區(qū)別在于,可以通過直接點擊兩側(cè)的幻燈片進行切換。

<template>
  <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>
</template>

<style>
  .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>

方向

默認(rèn)情況下,direction 為 horizontal。通過設(shè)置 direction 為 vertical 來讓走馬燈在垂直方向上顯示。


<template>
  <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>
</template>

<style>
  .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>

Carousel Attributes

參數(shù)說明類型可選值默認(rèn)值
height走馬燈的高度string
initial-index初始狀態(tài)激活的幻燈片的索引,從 0 開始number0
trigger指示器的觸發(fā)方式stringclick
autoplay是否自動切換booleantrue
interval自動切換的時間間隔,單位為毫秒number3000
indicator-position指示器的位置stringoutside/none
arrow切換箭頭的顯示時機stringalways/hover/neverhover
type走馬燈的類型stringcard
loop是否循環(huán)顯示boolean-true
direction走馬燈展示的方向stringhorizontal/verticalhorizontal
pause-on-hover鼠標(biāo)懸浮時暫停自動切換boolean-true

Carousel Events

事件名稱說明回調(diào)參數(shù)
change幻燈片切換時觸發(fā)目前激活的幻燈片的索引,原幻燈片的索引

Carousel Methods

方法名說明參數(shù)
setActiveItem手動切換幻燈片需要切換的幻燈片的索引,從 0 開始;或相應(yīng) el-carousel-item 的 name 屬性值
prev切換至上一張幻燈片
next切換至下一張幻燈片

Carousel-Item Attributes

參數(shù)說明類型可選值默認(rèn)值
name幻燈片的名字,可用作 setActiveItem 的參數(shù)string
label該幻燈片所對應(yīng)指示器的文本string


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號