added horizontal dots

This commit is contained in:
Rijn 2017-01-17 14:29:51 -06:00
parent 373dfb3cad
commit e9989f2b85
3 changed files with 189 additions and 47 deletions

View file

@ -1,20 +1,26 @@
<template> <template>
<div :class="classes"> <div :class="classes">
<div :class="[prefixCls + '-arrow', 'left']" @click="add(-1)"> <button :class="arrowClasses" class="left" @click="add(-1)">
<div class='placeholder'></div> <Icon type="chevron-left"></Icon>
<Icon type="arrow-left-b"></Icon> </button>
</div>
<div :class="[prefixCls + '-list']"> <div :class="[prefixCls + '-list']">
<div :class="[prefixCls + '-track']" :style="trackStyles" v-el:slides> <div :class="[prefixCls + '-track']" :style="trackStyles" v-el:slides>
<!-- opacity: 1; width: 4480px; transform: translate3d(-1120px, 0px, 0px); --> <!-- opacity: 1; width: 4480px; transform: translate3d(-1120px, 0px, 0px); -->
<slot></slot> <slot></slot>
</div> </div>
</div> </div>
<div :class="[prefixCls + '-arrow', 'right']" @click="add(1)"> <button :class="arrowClasses" class="right" @click="add(1)">
<div class='placeholder'></div> <Icon type="chevron-right"></Icon>
<Icon type="arrow-right-b"></Icon> </button>
</div> <ul :class="dotsClasses">
<!-- dots --> <template v-for="n in slides.length">
<li :class="{ [`${prefixCls}-active`]: n === currentIndex }"
@click="dotsEvent('click', n)"
@mouseover="dotsEvent('hover', n)">
<button></button>
</li>
</template>
</ul>
</div> </div>
</template> </template>
<script> <script>
@ -26,9 +32,9 @@
export default { export default {
name: 'Carousel', name: 'Carousel',
props: { props: {
arrows: { arrow: {
type: Boolean, type: String,
default: false default: 'hover'
}, },
autoplay: { autoplay: {
type: Boolean, type: Boolean,
@ -43,8 +49,12 @@
default: 'ease' default: 'ease'
}, },
dots: { dots: {
type: Boolean, type: String,
default: true default: 'inside'
},
trigger: {
type: String,
default: 'click'
}, },
vertical: { vertical: {
type: Boolean, type: Boolean,
@ -66,7 +76,6 @@
timer: null timer: null
} }
}, },
// events: before-change(from, to), after-change(current, from)
computed: { computed: {
classes () { classes () {
return [ return [
@ -82,6 +91,23 @@
transform: `translate3d(-${this.trackLeft}px, 0px, 0px)`, transform: `translate3d(-${this.trackLeft}px, 0px, 0px)`,
transition: `transform 500ms ${this.easing}` transition: `transform 500ms ${this.easing}`
}; };
},
arrowClasses () {
return [
`${prefixCls}-arrow`,
`${prefixCls}-arrow-${this.arrow}`
]
},
dotsClasses () {
return [
`${prefixCls}-dots`,
`${prefixCls}-arrow-${this.dots}`
]
},
activeDot (n) {
return {
[`${prefixCls}-vertical`]: this.currentIndex === n
}
} }
}, },
methods: { methods: {
@ -156,10 +182,14 @@
index += offset; index += offset;
while (index < 0) index += this.slides.length; while (index < 0) index += this.slides.length;
index = index % this.slides.length; index = index % this.slides.length;
this.$emit('on-change', this.currentIndex, index);
this.currentIndex = index; this.currentIndex = index;
}, },
slide () { dotsEvent (event, n) {
this.trackLeft = this.currentIndex * this.listWidth; if (event === this.trigger) {
this.$emit('on-change', this.currentIndex, n);
this.currentIndex = n;
}
}, },
setAutoplay () { setAutoplay () {
window.clearInterval(this.timer); window.clearInterval(this.timer);
@ -182,7 +212,7 @@
}, },
currentIndex () { currentIndex () {
this.$nextTick(() => { this.$nextTick(() => {
this.slide(); this.trackLeft = this.currentIndex * this.listWidth;
}); });
} }
}, },

View file

@ -41,40 +41,123 @@
} }
&-arrow { &-arrow {
position: absolute;
top: 0;
bottom: 0;
height: 100%;
text-align: center; border: none;
outline: none;
& > * { padding: 0;
display: inline-block; margin: 0;
vertical-align: middle;
}
.placeholder{ width: 36px;
overflow: hidden; height: 36px;
width: 0; border-radius: 50%;
min-height: inherit;
height: inherit;
}
z-index: 3;
&.left {
left: 0;
}
&.right {
right: 0;
}
width: 10%;
cursor: pointer; cursor: pointer;
display: none;
position: absolute;
top: 50%;
z-index: 10;
transform: translateY(-50%);
transition: .3s;
background-color: rgba(31, 45, 61, .11);
color: #fff;
&:hover { &:hover {
background: fade(#000, 30%); background-color: rgba(31, 45, 61, 0.5);
}
text-align: center;
font-size: 1em;
font-family: inherit;
line-height: inherit;
& > * {
vertical-align: baseline;
}
&.left {
left: 16px;
}
&.right {
right: 16px;
}
&-always {
display: inherit;
}
&-hover {
display: inherit;
opacity: 0;
}
}
&:hover &-arrow-hover {
opacity: 1;
}
&-dots {
@padding: 7px;
position: absolute;
bottom: 10px - @padding;
list-style: none;
display: block;
text-align: center;
padding: 0;
width: 100%;
height: 3px + @padding * 2;
li {
position: relative;
display: inline-block;
vertical-align: top;
text-align: center;
margin: 0 2px;
padding: @padding 0;
cursor: pointer;
button {
border: 0;
cursor: pointer;
background: #8391a5;
opacity: 0.3;
display: block;
width: 16px;
height: 3px;
border-radius: 1px;
outline: none;
font-size: 0;
color: transparent;
-webkit-transition: all .5s;
transition: all .5s;
}
&:hover > button {
opacity: 0.7;
}
&.@{carousel-prefix-cls}-active > button {
opacity: 1;
width: 24px;
}
} }
} }
} }

View file

@ -21,13 +21,39 @@
</i-col> </i-col>
<i-col span="4"> <i-col span="4">
<i-button @click="push">Push</i-button> <i-button @click="push">Push</i-button>
<i-button @click="remove = true">Remove</i-button> <i-button @click="remove = true">Remove Front</i-button>
</i-col>
<i-col span="4">
<p>Dots</p>
<Button-group>
<i-button @click="dots = 'inside'">Inside</i-button>
<i-button @click="dots = 'outside'">Outside</i-button>
<i-button @click="dots = 'none'">None</i-button>
</Button-group>
</i-col>
<i-col span="4">
<p>Trigger</p>
<Button-group>
<i-button @click="trigger = 'click'">Click</i-button>
<i-button @click="trigger = 'hover'">Hover</i-button>
</Button-group>
</i-col>
<i-col span="4">
Arrow
<Button-group>
<i-button @click="arrow = 'hover'">Hover</i-button>
<i-button @click="arrow = 'always'">Always</i-button>
<i-button @click="arrow = 'never'">Never</i-button>
</Button-group>
</i-col> </i-col>
</Row> </Row>
<Carousel style="width: 50%; border: solid 1px #000" <Carousel style="width: 50%; border: solid 1px #000"
:current-index.sync="currentIndex" :current-index.sync="currentIndex"
:autoplay="autoplay" :autoplay="autoplay"
:autoplay-speed="autoplaySpeed" :autoplay-speed="autoplaySpeed"
:dots="dots"
:trigger="trigger"
:arrow="arrow"
easing="linear"> easing="linear">
<Carousel-item v-if="!remove"> <Carousel-item v-if="!remove">
<Alert type="warning" show-icon> <Alert type="warning" show-icon>
@ -54,7 +80,10 @@
autoplay: true, autoplay: true,
autoplaySpeed: 2000, autoplaySpeed: 2000,
remove: false, remove: false,
pushItem: [] pushItem: [],
arrow: 'hover',
trigger: 'click',
dots: 'inside'
} }
}, },
methods: { methods: {