vue 幻燈片組件封裝

今天來(lái)做個(gè)幻燈組件的筆記,這里只記錄實(shí)現(xiàn)邏輯,不對(duì)樣式代碼做展示。
一個(gè)幻燈片組件需要實(shí)現(xiàn)的功能如下:
1、幻燈片的循環(huán)播放;
2、幻燈片分頁(yè);
3、幻燈片標(biāo)題的顯示;
4、鼠標(biāo)進(jìn)入,停止幻燈切換;
5、鼠標(biāo)離開(kāi),繼續(xù)幻燈切換;
6、點(diǎn)擊相應(yīng)幻燈,或幻燈標(biāo)題,觸發(fā)不同事件(根據(jù)實(shí)際需求做處理);
7、點(diǎn)擊頁(yè)碼,實(shí)現(xiàn)幻燈切換及循環(huán)播放。
效果如下:


具體步驟

第一步:新建slideShow.vue組件;
<template>
  <div>hello world {{ x }}</div>
</template>

<script>
  export default {
    name: "slideShow",
    data() {
      return {
        x : '我是slideShow'
      }
    }
  }
</script>
第二步:在父組件中引入
// template
<div class="index-right">
  <slide-show></slide-show>
</div>

// script
import slideShow from '../components/slideShow'
export default {
  components: {
    slideShow
  },
   data() {
      return {
      }
  }
}

這時(shí)候可以看到效果:slideShow組件的內(nèi)容已被渲染到父組件


第三步:在父組件為幻燈片動(dòng)態(tài)綁定要顯示的內(nèi)容,并在子組件接收。

知識(shí)點(diǎn):父、子組件通信。
父組件 :template

<slide-show :slides="slides"></slide-show> 

script :data

slides: [
        {
          src: require('../assets/slideShow/pic1.jpg'),
          title: 'xxx1',
          href: 'detail/analysis'
        },
        {
          src: require('../assets/slideShow/pic2.jpg'),
          title: 'xxx2',
          href: 'detail/count'
        },
        {
          src: require('../assets/slideShow/pic3.jpg'),
          title: 'xxx3',
          href: 'http://xxx.xxx.com'
        },
        {
          src: require('../assets/slideShow/pic4.jpg'),
          title: 'xxx4',
          href: 'detail/forecast'
        }
      ]

子組件,這里限制了父組件傳參的類型

 props: {
    slides: {
      type: Array,
      default: []
    }
  },
  mounted () {
    console.log(this.slides)
  }

這里可以看到,子組件接收到了父組件傳過(guò)來(lái)的參數(shù)



第四步:把父組件傳來(lái)的參數(shù)展示在模板;


<template>
  <div class="slide-show">
    <div class="slide-img">
      <a href="xxx">
          <img :src="slides[0].src">
      </a>
    </div>
    <h2>{{ slides[0].title }}</h2>
    <ul class="slide-pages">
      <li><</li>
      <li>1</li>
      <li>1</li>
      <li>1</li>
      <li>></li>
    </ul>
  </div>
</template>

效果如下



第五步:分頁(yè),讓幻燈片切換

<template>
  <div class="slide-show">
    <div class="slide-img">
      <a :href="slides[nowIndex].href">
        <img :src="slides[nowIndex].src">
      </a>
    </div>
    <h2>{{ slides[nowIndex].title }}</h2>
    <ul class="slide-pages">
      <li>&lt;</li>
      <li v-for="(item, index) in slides" @click="goto(index)">
        <a>{{ index + 1 }}</a>
      </li>
      <li>&gt;</li>
    </ul>
  </div>
</template>

    methods: {
      goto(index) {
        this.nowIndex = index
      }
    },

實(shí)現(xiàn)頁(yè)碼上一頁(yè)、下一頁(yè)功能;使用計(jì)算屬性,通過(guò)一個(gè)方法實(shí)現(xiàn);
知識(shí)點(diǎn):計(jì)算屬性。

<li @click="goto(prevIndex)">&lt;</li>
<li v-for="(item, index) in slides" @click="goto(index)">
  <a :class="{on: index === nowIndex}">{{ index + 1 }}</a>
</li>
<li @click="goto(nextIndex)">&gt;</li>
      
  computed: {
    prevIndex () {
      if (this.nowIndex === 0) {
        return this.slides.length - 1
      }
      else {
        return this.nowIndex - 1
      } 
    },
    nextIndex () {
      if (this.nowIndex === this.slides.length - 1) {
        return 0
      }
      else {
        return this.nowIndex + 1
      }
    }
  },

第六步:讓幻燈片自動(dòng)切換
父組件動(dòng)態(tài)傳參:幻燈片自動(dòng)切換時(shí)間間隔

<slide-show :slides="slides" :inv="invTime"></slide-show>

// 在data中注冊(cè)
invTime: 2000,

子組件

// props 注冊(cè)組件的動(dòng)態(tài)屬性
    inv: {
      type: Number,
      default: 1000
    }
    
// 添加 methods實(shí)現(xiàn)幻燈片切換
    runInv () {
      this.invId = setInterval(() => {
        this.goto(this.nextIndex)
      }, this.inv)
    },
// 在mounted中調(diào)用方法
  mounted () {
    this.runInv();
  }

第七步:鼠標(biāo)事件

// 在methods注冊(cè)一個(gè)清除切換的方法
    clearInv () {
      clearInterval(this.invId)
    }

// 在根節(jié)點(diǎn)添加鼠標(biāo)時(shí)間
<template>
  <div class="slide-show" @mouseover="clearInv" @mouseout="runInv">
...

第八步:高級(jí)功能:圖片的動(dòng)畫效果
知識(shí)點(diǎn):樣式標(biāo)簽transition的使用;
難點(diǎn):這里的兩個(gè)nowIndex一個(gè)是新值一個(gè)是舊值,在goto函數(shù)中改變。

// 幻燈片切換添加transition
      <a :href="slides[nowIndex].href">
        <transition name="slide-trans">
          <img v-if="isShow" :src="slides[nowIndex].src">
        </transition>
        <transition name="slide-trans-old">
          <img v-if="!isShow" :src="slides[nowIndex].src">
        </transition>
      </a>
      
// 為變量注冊(cè)初始值
isShow:true

// 修改goto函數(shù)
      goto(index) {
        this.isShow = false
        setTimeout(() => {
          this.isShow = true
          this.nowIndex = index
        }, 10)
      },

// 添加樣式
  .slide-trans-enter-active {
    transition: all .5s;
  }
  .slide-trans-enter {
    transform: translateX(900px);
  }
  .slide-trans-old-leave-active {
    transition: all .5s;
    transform: translateX(-900px);
  }

第九步:子組件切換:觸發(fā)父組件的事件
知識(shí)點(diǎn):子組件向父組件通信

// 所有的切換都是在goto函數(shù)中,所以在goto函數(shù)調(diào)用事件$emit
      goto(index) {
        this.isShow = false
        setTimeout(() => {
          this.isShow = true
          this.nowIndex = index
          this.$emit('onChange',index)
        }, 10)
      },
// 在父組件的子組件標(biāo)簽中綁定事件
<slide-show :slides="slides" :inv="invTime" @onChange="doSomethingOnSlideChange"></slide-show>
// 在methods中實(shí)現(xiàn)綁定的事件
    methods: {
      doSomethingOnSlideChange() {
        console.log('幻燈片切換了,父組件要做點(diǎn)什么')
      }
    },
?著作權(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)容

  • 前言 您將在本文當(dāng)中了解到,往網(wǎng)頁(yè)中添加數(shù)據(jù),從傳統(tǒng)的dom操作過(guò)渡到數(shù)據(jù)層操作,實(shí)現(xiàn)同一個(gè)目標(biāo),兩種不同的方式....
    itclanCoder閱讀 26,238評(píng)論 1 12
  • “這是第三遍看這部電影。我自己覺(jué)得很好,我還要再看幾遍?!?這句話來(lái)自馬云,其所褒獎(jiǎng)的對(duì)象是上個(gè)月剛剛榮獲奧斯卡大...
    玉辰弓子閱讀 473評(píng)論 0 6
  • 我所有的自負(fù)都來(lái)自我的自卑,所有的英雄氣概都來(lái)自于我內(nèi)心的軟弱,所有的振振有詞都因?yàn)樾闹袧M是懷疑。我假裝無(wú)情...
    即刻至上閱讀 516評(píng)論 0 1
  • 古人常說(shuō),“讀書破萬(wàn)卷,下筆如有神”,“書中自有黃金屋,書中自有顏如玉”。讀書自古以來(lái)就是一件美好的事情。一本好書...
    把你賣了買糖吃閱讀 662評(píng)論 0 1
  • 今天中午跟溫森又是一戰(zhàn),我真是找不到原因,說(shuō)好的睡覺(jué),他也躺下了,就是中途又哭了,不明原因的哭,就是哭,我真是不知...
    Vincen媽媽閱讀 139評(píng)論 0 0

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