幻灯片切换的方向能够变化么?

来源:6-7 首页 - 幻灯片组件(2)

死亡之翼death

2017-08-21

这样的轮播图,无论点前进还是后退,都是从右向左滑入呢。

有没有办法我点后退是从右向左,点前景是从左向右?

写回答

1回答

死亡之翼death

提问者

2017-08-21

自己搞定了,给data中增加一个变量direction。为<transition>的name设置绑定,name值是变量,会根据direction而变化。

具体如下

html代码:

<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>

script:

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
    }
  },

css(less)

.carousel-trans-toleft-enter-active,.carousel-trans-toleft-old-leave-active{
  transition:all 0.5s;
}
.carousel-trans-toleft-enter{
  transform:translateX(940px); //新图片从右侧940px进入
}
.carousel-trans-toleft-old-leave-active{
  transform:translateX(-940px);  //老图片向左侧940px出去
}
.carousel-trans-toright-enter-active,.carousel-trans-toright-old-leave-active{
  transition:all 0.5s;
}
.carousel-trans-toright-enter{
  transform:translateX(-940px); //新图片从右侧940px进入
}
.carousel-trans-toright-old-leave-active{
  transform:translateX(940px);  //老图片向左侧940px出去
}


3
0

最容易上手的Vue2.0入门实战教程

快速入门Vue2.0,组件化开发一个数字产品电商平台

3966 学习 · 999 问题

查看课程