Vue項(xiàng)目中【封裝復(fù)用】使用elementUI中的Carousel 走馬燈實(shí)現(xiàn)輪播圖效果

簡(jiǎn)述:

在項(xiàng)目中,使用輪播的地方有多處,所以做了一個(gè)簡(jiǎn)單的封裝
效果如Carousel 走馬燈實(shí)現(xiàn)輪播圖效果一致

封裝組件

子組件

<template>
  <!-- 輪播圖片 -->
  <div class="carousel">
    <el-carousel :height="height+'px'"  :loop="true" :interval="interval" trigger="click" arrow="always">
      <el-carousel-item :width="width+'px'" class="image" v-for="item in carouselList" :key="item.id">
        <img :src="item.url"  :width="widthImage+'%'" :height="heightImage+'%'" alt="無(wú)圖片"/>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script>
  export default {
    name: "Carousel",
//接收來(lái)自父組件的數(shù)據(jù)
    props: {
      // 輪播圖寬度
      width: {
        type: Number,
        default:0
      },
      height: {
        type: String,
        default: '400'
      },
      // 相鄰兩張圖片切換的間隔時(shí)間
      interval: {
        type: Number,
        default: 2000
      },
        //圖片寬
      widthImage:{
        type: Number,
        default: 100
      },
      //圖片高
      heightImage:{
        type: Number,
        default: 100
      },
      // 輪播圖路徑數(shù)組
      carouselList: {
        type: Array,
        default: function(){
          return [];
        }
      }
    },
    data() {
      return { };
    },
  };
</script>

<style scoped>
  .el-carousel {
    overflow-x: hidden;
    overflow-y: hidden;
  }

  .el-carousel__item {
    background-color: #99a9bf;
  }

  .image {
    height: 550px;
  }
</style>

父組件:
引用封裝好的子組件

<template>
 <Carousels :height="height" :width="1000" :carouselList="images"></Carousels>
</template>

<script>
  import Carousels from './Carousels'
export default {
    components: {
      Carousels ,
    },
  data() {
      return {
        height:'450',
       images: [{
            id: 1,
            url: require("@/assets/ad1.jpg")
          },
          {
            id: 2,
            url: require("@/assets/ad2.jpg")
          },
          {
            id: 3,
            url: require("@/assets/ad3.jpg")
          },
          {
            id: 4,
            url: require("@/assets/ad4.jpg")
          },
          {
            id: 5,
            url: require("@/assets/ad5.jpg")
          }
        ],
  }
}
    },
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容