1+ <template >
2+ <div class =" layui-carousel" lay-anim =" " lay-indicator =" inside" :lay-arrow =" arrow"
3+ style =" width : 600px ;" :style =" {
4+ height: height + 'px'
5+ }" >
6+ <div carousel-item =" " >
7+ <slot ></slot >
8+ </div >
9+ <div class =" layui-carousel-ind" >
10+ <ul >
11+ <li v-for =" item in items" :class =" {'layui-this': item.isActive}" ></li >
12+ </ul >
13+ </div >
14+ <button class =" layui-icon layui-carousel-arrow" @click =" handleSub" lay-type =" sub" ></button >
15+ <button class =" layui-icon layui-carousel-arrow" @click =" handleAdd" lay-type =" add" ></button >
16+ </div >
17+ </template >
18+
19+ <script >
20+ export default {
21+ name: " LayCarousel" ,
22+ data () {
23+ return {
24+ activeItem: ' ' ,
25+ nextItem: ' ' ,
26+ prevItem: ' ' ,
27+ items: [],
28+ righting: false ,
29+ lefting: false
30+ }
31+ },
32+ props: {
33+ arrow: {
34+ type: String ,
35+ default: ' always'
36+ },
37+ height: {
38+ type: Number ,
39+ default: 280
40+ }
41+ },
42+ mounted () {
43+
44+ },
45+ methods: {
46+ handleSub () {
47+ if (this .lefting ) {
48+ return false
49+ }
50+ this .lefting = true
51+ this .updateItem ()
52+ setTimeout (() => {
53+
54+ this .lefting = false
55+ const activeIndex = this .items .findIndex (o => o === this .activeItem )
56+ if (activeIndex == 0 ) {
57+ this .setActiveItem (this .items [this .items .length - 1 ])
58+ return false ;
59+ }
60+ this .setActiveItem (this .items [activeIndex - 1 ])
61+ }, 300 )
62+ },
63+ handleAdd () {
64+ if (this .righting ) {
65+ return false
66+ }
67+ this .righting = true
68+ this .updateItem ()
69+ setTimeout (() => {
70+ this .righting = false
71+ const activeIndex = this .items .findIndex (o => o === this .activeItem )
72+ if (activeIndex == this .items .length - 1 ) {
73+ this .setActiveItem (this .items [0 ])
74+ return false ;
75+ }
76+ this .setActiveItem (this .items [activeIndex + 1 ])
77+
78+ }, 300 )
79+
80+ },
81+ updateItems () {
82+ this .items = this .$children .filter (child => child .$options .name === ' LayCarouselItem' );
83+ this .setActiveItem (this .items [0 ])
84+ },
85+ updateItem () {
86+ this .items .forEach (o => {
87+ o .isActive = o == this .activeItem
88+ o .isPrev = o == this .prevItem
89+ o .isNext = o == this .nextItem
90+ o .isRighting = this .righting
91+ o .isLefting = this .lefting
92+ })
93+ },
94+ setActiveItem (item ) {
95+ this .activeItem = item
96+ const activeIndex = this .items .findIndex (o => o === this .activeItem )
97+
98+ this .nextItem = activeIndex == this .items .length - 1 ? this .items [0 ] : this .items [activeIndex + 1 ]
99+ this .prevItem = activeIndex == 0 ? this .items [this .items .length - 1 ] : this .items [activeIndex - 1 ]
100+
101+ this .updateItem ()
102+ }
103+ }
104+ }
105+ </script >
106+
107+ <style scoped>
108+
109+ </style >
0 commit comments