成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何使用vue.js實現(xiàn)輪播

小編給大家分享一下如何使用vue.js實現(xiàn)輪播,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

大足ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

使用vue.js實現(xiàn)輪播的方法:首先使用“<transition name="targetClassName">”將相應(yīng)的元素包裹??;然后在“.imgShoudMove”中設(shè)置動畫屬性;最后采用Vue結(jié)合Css3來實現(xiàn)輪播圖即可。

首先要了解的是Vue的動畫原理。在vue中,如果我們要給元素設(shè)置動畫效果,則需要使用一個<transition name="targetClassName"></transition>將相應(yīng)的元素包裹住,如下:

<transition name="imgShouldMove"> 
 <img v-if="shouldShow" src="/1.jpg"> 
</transition>

之后,便可以在.imgShoudMove中設(shè)置動畫屬性了,如下:

.imgShouldMove-enter{ 
 transition: all 0.5s; 
} 
.imgShouldMove-enter-active{ 
 transform:translateX(900px); 
}

注意在HTML中,這里<img>有一個v-if="shoudShow"屬性。shouldShow這個屬性是在data(){}中設(shè)置的,當(dāng)shouldShow從false-->true時(即img從無到突然出現(xiàn)時),

Vue動畫原理將動畫分為了 shouldShouldMove-enter 和 imgShouldMove-enter-active 兩個階段。

其中 shouldShouldMove-enter 表示動畫開始的初始狀態(tài), imgShouldMove-enter-active 這表示動畫的終止?fàn)顟B(tài)。而動畫的觸發(fā)則是通過if-show引起的。

示例:

HTML代碼:

<template>
 <div class="carousel" @mouseenter="clearInv()" @mouseleave="runInterval()">
 <div class="imgBox">
 <a :href="pics[currentIndex].href" rel="external nofollow" >
 <transition v-bind:name="'carousel-trans-' + direction + '-old'">
 <!-- isShow: false -> true
 取反后: true -> false(從顯示到消失) -->
  <img v-if="!isShow" :src="pics[currentIndex].src">
 </transition>
 <transition v-bind:name="'carousel-trans-' + direction ">
 <!-- isShow: false -> true -->
 <!-- 從消失到顯示 -->
  <img v-if="isShow" :src="pics[currentIndex].src">
 </transition>
 </a>
 </div>
 <h3>{{pics[currentIndex].title}}</h3>
 <ul class="pagination">
 <li v-for="(item, index) in pics" @click="goto(index)" :
 class="{active:index === currentIndex}">{{index + 1}}</li>
 </ul>
 <div class="prevBtn" @click="goto(prevIndex)"><i class="iconfont"></i></div>
 <div class="nextBtn" @click="goto(nextIndex)"><i class="iconfont"></i></div>
 </div>
</template>
Script代碼:
<script>
export default {
 props:{
 pics:{
 type:Array,
 default:[]
 },
 timeDelta:{
 type:Number,
 default:2000
 }
 },
 data () {
 return {
 currentIndex:0,
 isShow:true,
 direction:'toleft'
 }
 },
 computed:{
 prevIndex(){
 this.direction = 'toleft'
 if (this.currentIndex <= 0) {
 return this.pics.length - 1
 }
 return this.currentIndex - 1
 },
 nextIndex(){
 this.direction = 'toright'
 if (this.currentIndex >= this.pics.length - 1) {
 return 0
 }
 return this.currentIndex + 1
 }
 },
 methods:{
 goto(index){
 this.isShow = false
 setTimeout(()=>{
 this.isShow = true
 this.currentIndex = index
 },10)
  
 },
 runInterval(){
 this.direction = 'toright'
 this.timer = setInterval(()=>{
 this.goto(this.nextIndex)
 },this.timeDelta)
 },
 clearInv(){
 clearInterval(this.timer)
 }
 },
 mounted(){
 this.runInterval()
 }
}
</script>

與動畫相關(guān)的css代碼如下

.carousel-trans-toright-enter-active,
.carousel-trans-toright-old-leave-active{ 
 transition:all 0.5s; 
} 
.carousel-trans-toright-enter{ 
 transform:translateX(940px);
  //新圖片從右側(cè)940px進(jìn)入 
} 
.carousel-trans-toright-old-leave-active{ 
 transform:translateX(-940px);
  //老圖片向左側(cè)940px出去 
} 
.carousel-trans-toleft-enter-active,
.carousel-trans-toleft-old-leave-active{ 
 transition:all 0.5s; 
} 
.carousel-trans-toleft-enter{ 
 transform:translateX(-940px); 
 //新圖片從右側(cè)940px進(jìn)入 
} 
.carousel-trans-toleft-old-leave-active{ 
 transform:translateX(940px); 
 //老圖片向左側(cè)940px出去 
}

注意:對于<img>需要放在<box>里面,<box>需要設(shè)置為position:relative; 而<img>必須設(shè)置為position:absolute; 這步非常非常重要,否則每次莫名其妙的總是只有一張圖片顯示。

在每次切換的時候,都要觸發(fā)goto()方法,將this.isShow先置false,10毫秒后,this.isShow置true。這時,html中的<transition>被觸發(fā),它與css相結(jié)合觸發(fā)動畫效果,持續(xù)時間為css屬性中的transition所定的0.5s。

在向前、向后切換的時候,使用到了計算屬性,在div.prevBtn以及div.nextBtn上,我們作了點擊事件綁定,觸發(fā)方法goto(),而傳入的正是計算屬性prevIndex, @click="goto(prevIndex)"

計算屬性的設(shè)定方法如下:

computed:{ 
 prevIndex(){ 
 //經(jīng)過一番計算過程得出result 
 return result //這個值即<template>中的prevIndex 
 } 
 },

每隔2秒自動滑動時,我們向left滑動,在data中,設(shè)定了變量 direction ,它的值要么為字符串'toleft',要么為'toright'。

我們在計算屬性中對 this.direction 進(jìn)行了設(shè)置,并在<template>中對相應(yīng)的name進(jìn)行了字符串拼接,如下

<transition v-bind:name="'carousel-trans-' + direction ">

在vue中,除了class和style可以傳入對象、數(shù)組,其他的屬性綁定必須進(jìn)行字符串拼接。

看完了這篇文章,相信你對如何使用vue.js實現(xiàn)輪播有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

本文名稱:如何使用vue.js實現(xiàn)輪播
文章源于:http://jinyejixie.com/article42/podcec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站設(shè)計、移動網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、定制網(wǎng)站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)