使用vue.js实现轮播的方法:首先使用“<transition name="targetClassName">”将相应的元素包裹住;然后在“.imgShoudMove”中设置动画属性;最后采用Vue结合Css3来实现轮播图即可。

本教程操作环境:windows7系统、vue2.0&&css3版,该方法适用于所有品牌电脑。 推荐:《vue教程》 本文章采用Vue结合Css3来实现轮播图。 首先要了解的是Vue的动画原理。在vue中,如果我们要给元素设置动画效果,则需要使用一个<transition name="targetClassName"></transition>将相应的元素包裹住,如下: <transition name="imgShouldMove">
<img v-if="shouldShow" src="/1.jpg">
</transition> 之后,便可以在.imgShoudMove中设置动画属性了,如下: .imgShouldMove-enter{
transition: all 0.5s;
}
.imgShouldMove-enter-active{
transform:translateX(900px);
}注意在HTML中,这里<img>有一个v-if="shoudShow"属性。shouldShow这个属性是在data(){}中设置的,当shouldShow从false-->true时(即img从无到突然出现时), Vue动画原理将动画分为了 shouldShouldMove-enter 和 imgShouldMove-enter-active 两个阶段。 其中 shouldShouldMove-enter 表示动画开始的初始状态, imgShouldMove-enter-active 这表示动画的终止状态。而动画的触发则是通过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>
<h2>{{pics[currentIndex].title}}</h2>
<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"> |