2016-12-22 08:10:55 +00:00
|
|
|
<template>
|
|
|
|
<div
|
|
|
|
v-show="ready"
|
|
|
|
class="el-carousel__item"
|
|
|
|
:class="{
|
|
|
|
'is-active': active,
|
2016-12-23 08:19:39 +00:00
|
|
|
'el-carousel__item--card': $parent.type === 'card',
|
2016-12-22 08:10:55 +00:00
|
|
|
'is-in-stage': inStage
|
|
|
|
}"
|
|
|
|
@click="handleItemClick"
|
2016-12-23 08:19:39 +00:00
|
|
|
:style="{
|
|
|
|
msTransform: `translateX(${ translate }px) scale(${ scale })`,
|
|
|
|
webkitTransform: `translateX(${ translate }px) scale(${ scale })`,
|
|
|
|
transform: `translateX(${ translate }px) scale(${ scale })`
|
|
|
|
}">
|
2016-12-22 08:10:55 +00:00
|
|
|
<div
|
2016-12-23 08:19:39 +00:00
|
|
|
v-if="$parent.type === 'card'"
|
2016-12-22 08:10:55 +00:00
|
|
|
v-show="!active"
|
|
|
|
class="el-carousel__mask">
|
|
|
|
</div>
|
|
|
|
<slot></slot>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const CARD_SCALE = 0.83;
|
|
|
|
export default {
|
|
|
|
name: 'ElCarouselItem',
|
|
|
|
|
2016-12-23 08:19:39 +00:00
|
|
|
props: {
|
|
|
|
name: String
|
|
|
|
},
|
|
|
|
|
2016-12-22 08:10:55 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
translate: 0,
|
|
|
|
scale: 1,
|
|
|
|
active: false,
|
|
|
|
ready: false,
|
|
|
|
inStage: false
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
processIndex(index, activeIndex, length) {
|
|
|
|
if (activeIndex === 0 && index === length - 1) {
|
|
|
|
return -1;
|
|
|
|
} else if (activeIndex === length - 1 && index === 0) {
|
|
|
|
return length;
|
|
|
|
} else if (index < activeIndex - 1 && activeIndex - index >= length / 2) {
|
|
|
|
return length + 1;
|
|
|
|
} else if (index > activeIndex + 1 && index - activeIndex >= length / 2) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
|
|
|
|
calculateTranslate(index, activeIndex, parentWidth) {
|
|
|
|
if (this.inStage) {
|
|
|
|
return parentWidth * ((2 - CARD_SCALE) * (index - activeIndex) + 1) / 4;
|
|
|
|
} else if (index < activeIndex) {
|
|
|
|
return -(1 + CARD_SCALE) * parentWidth / 4;
|
|
|
|
} else {
|
|
|
|
return (3 + CARD_SCALE) * parentWidth / 4;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
translateItem(index, activeIndex) {
|
|
|
|
const parentWidth = this.$parent.$el.offsetWidth;
|
|
|
|
const length = this.$parent.items.length;
|
|
|
|
|
2016-12-23 08:19:39 +00:00
|
|
|
if (this.$parent.type === 'card') {
|
2016-12-22 08:10:55 +00:00
|
|
|
if (index !== activeIndex && length > 2) {
|
|
|
|
index = this.processIndex(index, activeIndex, length);
|
|
|
|
}
|
|
|
|
this.inStage = Math.round(Math.abs(index - activeIndex)) <= 1;
|
|
|
|
this.active = index === activeIndex;
|
|
|
|
this.translate = this.calculateTranslate(index, activeIndex, parentWidth);
|
|
|
|
this.scale = this.active ? 1 : CARD_SCALE;
|
|
|
|
} else {
|
|
|
|
this.active = index === activeIndex;
|
|
|
|
this.translate = parentWidth * (index - activeIndex);
|
|
|
|
}
|
|
|
|
this.ready = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
handleItemClick() {
|
|
|
|
const parent = this.$parent;
|
2016-12-23 08:19:39 +00:00
|
|
|
if (parent && parent.type === 'card') {
|
2016-12-22 08:10:55 +00:00
|
|
|
const index = parent.items.indexOf(this);
|
2016-12-23 08:19:39 +00:00
|
|
|
parent.setActiveItem(index);
|
2016-12-22 08:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
this.$parent && this.$parent.handleItemChange();
|
|
|
|
},
|
|
|
|
|
|
|
|
destroyed() {
|
|
|
|
this.$parent && this.$parent.handleItemChange();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|