vue自定義封裝swiper組件

1.在components 創(chuàng)建下組件swiper.vue文件

<template>
    <div class="banner" >
          <div class="box">
             <ul>
                  <li v-for="(item, index) in list" :key="index" :class="index === current ? 'active' : ''" @click="bannerClick(index, item)"><img :src="item.url" alt=""></li>
              </ul>
              <div class="status">
                  <span v-for="(item, index) in list"  :key="index" :class="index === current ? 'active' : ''" 
                  @mouseenter="changeBanner(index)"
                  @mouseleave="startLoop"
                  >
              </span>
              </div>
              <div class="btn">
                  <span class="prev" @click="prev" @mouseenter="stopLoop" @mouseleave="startLoop"><icon-svg icon-class="iconjiantou" /></span>
                  <span class="next" @click="next" @mouseenter="stopLoop" @mouseleave="startLoop"><icon-svg icon-class="iconjiantou" /></span>
              </div> -->
          </div>
      </div>
</template>

<script>
import IconSvg from './icon-components.vue'
export default {
  components:{IconSvg},
  name: 'swiper',
  data() {
    return {
        current: 0, // 當(dāng)前索引
        timerId: null, // 清除循環(huán)標(biāo)記
        intStyle: {}
    }
  },
  props: [
      'list', 
      'looptime', 
      'height', 
      'width', 
      'background', 
      'color',
      'fontSize'
  ],
  mounted(){
  },
  methods: {
    getArticle () {
        this.$emit('getArticle', this.article)
    },
    // 鼠標(biāo)移入狀態(tài)圓點(diǎn)
    changeBanner (index) {
        this.$emit('change', this.current);
        this.stopLoop();
        this.current = index;
    },
    // 鼠標(biāo)點(diǎn)擊banner內(nèi)容
    bannerClick (index, item) {
        this.$emit('click', index, item);
    },
    // 點(diǎn)擊上一張按鈕
    prev () {
        if(this.current > 0) {
            // 將對象列表對應(yīng)的索引和整個(gè)對象傳出去
            this.$emit('prev', this.current, this.list);
            this.current--;
        } else {
            this.$emit('prev', this.current, this.list);
            this.current = 3;
        }
    },
    // 點(diǎn)擊下一張按鈕
    next () {
        if(this.current < 3) {
            this.$emit('prev', this.current, this.list);
            this.current ++;
        } else {
            this.$emit('prev', this.current, this.list);
            this.current = 0;
        }
    },
    // 鼠標(biāo)移出繼續(xù)循環(huán)播放
    startLoop () {
        this.int(4000);
    },
    // 鼠標(biāo)移入停止循環(huán)播放
    stopLoop () {
        clearTimeout(this.timerId); // 清除循環(huán)
    },
    // 初始化加載
    int (time){
        this.timerId = setInterval(()=> {
          this.next();
      }, time);
      // 初始化樣式
      this.intStyle = {
        width: this.width + 'px',
        height: this.height + 'px',
        background: this.background,
        color: this.color
      }
    }
  },
  created () {
    this.int(4000);
  }
}
</script>
<style lang="stylus" scoped>
.banner{
    width:900px;
    height:434px;
    overflow:hidden;
    .box{
        position:relative;
        height:100%;
        ul{
            height:100%;
            margin 0
            li{
                position:absolute;
                left:0;
                width:100%;
                height:100%;
                font-size:inherit;
                color:#fff;
                font-size:80px;
                text-align:center;
                opacity:0;
                transition: all 1.5s;
                img{
                  width: 100%;
                  height: 100%;
                }
            }
            .active{
                opacity:1;
                transition: all 1.5s;
            }
        }
        .status{
            position:absolute;
            bottom:0;
            width:100%;
            height:40px;
            text-align:Center;
            span{
                display:inline-block;
                height:17px;
                width:17px;
                margin:0 5px;
                background:#fff;
                border-radius:10px;
                color:#333;
                cursor:pointer;
            }
            span.active{
                color:#fff;
                background:#db6f78;
            }
        }
    }
    .btn{
        position:absolute;
        top:50%;
        width:100%;
        transform: translateY(-50%);
        span{
            display:block;
            height:70px;
            width:58px;
            color:#fff;
            line-height:70px;
            text-align:Center;
            background:rgba(0,0,0, 0.3);
            cursor:pointer;
        }
        span.prev{
            float:left;
        }
        span.next{
            float:right;
        }
    }
}
</style>

2.在需要使用這個(gè)組件的頁面 引入swiper組件

import banner from '../components/Swiper.vue';

components:{banner}

3.在需要使用swiper的位置寫如下代碼

      <banner
           :list="list"
           :looptime="looptime"
            width="width"
           :height="height"
           :background="background"
           :color="color"
           :fontSize="fontSize"
         >
     </banner>

上面用到變量都在data里定義賦值就可以了(vue2)(vue3寫在return里面)

looptime: 4000, // 循環(huán)時(shí)間
      width: 400,
      height: 200,
      background: 'red',
      color: '#fff',
      fontSize: '70px',
  
      list: [
        {
          id: 1,
          text: '1',
          url:
            'http://CgEUe176rnSAbneHAAFYHWnAzQs453.jpg',
        },
        {
          id: 2,
          text: '2',
          url:
            'https://CgEhS17-ttSAbrC8AALhs6ZdoX8724.jpg',
        },
        {
          id: 3,
          text: '3',
          url:
            'https:CgEhTl78CHSAXCMhAAG3yt1ilmk722.jpg',
        },
        {
          id: 4,
          text: '4',
          url:
            'https://CgEhTl78CByAcsaMAAJqZ6Z2uIs537.jpg',
        },
      ],
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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