使用vue.js如何实现轮播


当前第2页 返回上一页

HTML代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

<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">?\</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>

与动画相关的css代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

.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出去

}

.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出去

}

注意:对于<img>需要放在<box>里面,<box>需要设置为position:relative; 而<img>必须设置为position:absolute; 这步非常非常重要,否则每次莫名其妙的总是只有一张图片显示。

在每次切换的时候,都要触发goto()方法,将this.isShow先置false,10毫秒后,this.isShow置true。这时,html中的<transition>被触发,它与css相结合触发动画效果,持续时间为css属性中的transition所定的0.5s。

在向前、向后切换的时候,使用到了计算属性,在div.prevBtn以及div.nextBtn上,我们作了点击事件绑定,触发方法goto(),而传入的正是计算属性prevIndex, @click="goto(prevIndex)"

计算属性的设定方法如下:

1

2

3

4

5

6

computed:{

 prevIndex(){

 //经过一番计算过程得出result

 return result //这个值即<template>中的prevIndex

 }

 },

每隔2秒自动滑动时,我们向left滑动,在data中,设定了变量 direction ,它的值要么为字符串'toleft',要么为'toright'。

我们在计算属性中对 this.direction 进行了设置,并在<template>中对相应的name进行了字符串拼接,如下

1

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

在vue中,除了class和style可以传入对象、数组,其他的属性绑定必须进行字符串拼接。

推荐:《vue教程》

以上就是使用vue.js如何实现轮播的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

vue收费的吗

vue计算属性和watch的区别有哪些?

vue中mounted和created的区别(图文详解)

vue返回上一页效果(this.$router.go)

vue怎么调用jquery包

vue 3.2发布啦,站起来继续学!

vue和js区别是什么

关于vue使用验证器: veevalidate3

详解vue中动态添加类名的方法

vue-loader是什么?

更多相关阅读请进入《vue》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...