1. 程式人生 > 其它 >vue幻燈片元件(一頁多圖)

vue幻燈片元件(一頁多圖)

加班到類似,就不一一介紹了,話不多說,直接上程式碼

<template>
  <div class="slider-body" ref="body">
    <div class="toLeft" @click="toLeft">
      <a-icon type="left"></a-icon>
    </div>
    <div class="slide-content" ref="content">
      <!-- 就突出一個套娃 -->
      <div
        
class="slide-chunk" v-for="(chunk, chunkindex) in Img_arr" :key="chunkindex" :style="chunk.length == 1 ? 'justify-content:center' : ''" > <el-image v-for="(item, index) in chunk" fit="cover" :src="img_url + item.filePath" :key="index"> </el-image> </
div> </div> <div class="toRight" @click="toRight"> <a-icon type="right"></a-icon> </div> </div> </template> <script> export default { props: { //圖片陣列 imglist: { type: Array, default() { return [] }, },
//橫向圖片的數量(目前還沒想好怎麼給他弄成可以自定義幾張的佈局,目前只支援兩張) hengxiang_number: { type: Number, default() { return 2 }, }, //是否自動滾動 autoplay: { type: Boolean, default() { return false }, }, //自動滾動時長 playtime: { type: Number, default() { return 3000 }, }, }, data() { return { img_url: process.env.VUE_APP_API_ASSET_URL, Img_arr: [], content_span: 0, } }, watch: { imglist: { deep: true, handler(v) { this.Img_arr = [] this.listFilter() }, }, }, mounted() { this.listFilter() let _this = this if (_this.autoplay) { setInterval(function () { let move_dis = _this.$refs.body.clientWidth let content_length = move_dis * _this.Img_arr.length console.log(_this.$refs) if (Math.abs(_this.content_span) !== content_length - move_dis) { _this.content_span -= move_dis _this.$refs.content.style.transform = `translateX(${_this.content_span}px)` } else { _this.content_span = 0 } }, _this.playtime) } }, methods: { //將圖片陣列切分為不同的塊(比如10張圖片,每頁2張,會被分割為5頁,11張會被分割為6頁) listFilter() { let data = this.imglist for (var i = 0; i < data.length; i += this.hengxiang_number) { this.Img_arr.push(data.slice(i, i + this.hengxiang_number)) } }, toLeft() { //移動距離 = 塊長度 let move_dis = this.$refs.body.clientWidth //滾動圖片列表的長度 = 塊長度*頁面長度 let content_length = move_dis * this.Img_arr.length console.log(this.content_span, content_length - move_dis) // 向左最大距離 = 滾動圖片列表的長度 - 最後一塊的寬度 if (Math.abs(this.content_span) !== content_length - move_dis) { this.content_span -= move_dis this.$refs.content.style.transform = `translateX(${this.content_span}px)` } }, toRight() { //同上 let move_dis = this.$refs.body.clientWidth // 向左最大距離為0 if (this.content_span != 0) { this.content_span += move_dis this.$refs.content.style.transform = `translateX(${this.content_span}px)` } }, }, } </script> <style lang="less" scoped> .slider-body { width: 100%; overflow: hidden; line-height: 0; margin-top: 16px; position: relative; .toLeft, .toRight { position: absolute; z-index: 2004; top: 50%; width: 10%; font-size: 40px; color: rgba(240, 240, 240, 0.8); transform: translateY(-20px); cursor: pointer; } .toLeft { left: 0; height: 100%; } .toRight { right: 0; height: 100%; text-align: end; } } .slide-content { display: inline-flex; width: 100%; transition: all 0.8s; .slide-chunk { width: 100%; line-height: 0; display: flex; flex-shrink: 0; justify-content: space-between; .el-image { width: 48%; height: 240px; flex-shrink: 0; height: 160px; line-height: 0; background: #dddddd; } } } </style>

還不是太完善,等我不懶的時候再改