swipe add indicator

This commit is contained in:
cookfront 2017-03-27 17:06:30 +08:00
parent a991aea581
commit 03af3b3eba
5 changed files with 99 additions and 34 deletions

View File

@ -53,13 +53,6 @@
</zan-col> </zan-col>
</zan-row> </zan-row>
</example-block><example-block title="button group">
<div class="button-group">
<zan-button type="primary" size="small">确认付款</zan-button>
<zan-button size="small">确认收货</zan-button>
<zan-button size="small">取消订单</zan-button>
</div>
</example-block></section></template> </example-block></section></template>
<style> <style>
@component-namespace demo { @component-namespace demo {

View File

@ -12,6 +12,16 @@
} }
</style> </style>
<script>
export default {
methods: {
handlePageEnd(page, index) {
console.log(page, index);
}
}
};
</script>
## Swipe 轮播 ## Swipe 轮播
### 基础用法 ### 基础用法
@ -33,7 +43,7 @@
:::demo 自动轮播 :::demo 自动轮播
```html ```html
<zan-swipe :auto-play="true"> <zan-swipe :auto-play="true" @pagechange:end="handlePageEnd">
<zan-swipe-item> <zan-swipe-item>
<img src="https://img.yzcdn.cn/upload_files/2017/03/14/FmTPs0SeyQaAOSK1rRe1sL8RcwSY.jpeg?imageView2/2/w/980/h/980/q/75/format/webp" alt=""> <img src="https://img.yzcdn.cn/upload_files/2017/03/14/FmTPs0SeyQaAOSK1rRe1sL8RcwSY.jpeg?imageView2/2/w/980/h/980/q/75/format/webp" alt="">
</zan-swipe-item> </zan-swipe-item>
@ -43,3 +53,16 @@
</zan-swipe> </zan-swipe>
``` ```
::: :::
### API
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|-----------|-----------|-----------|-------------|-------------|
| autoPlay | 是否自动轮播 | `boolean` | `false` | `true`, `false` |
| showIndicators | 是否显示指示器 | `boolean` | `true` | `true`, `false` |
### 事件
| 事件名 | 说明 | 参数 |
|-----------|-----------|-----------|
| `pagechange:end` | 每一页轮播结束后触发 | `(elem, currIndex)``elem`为触发页当前的DOM节点 |

View File

@ -6,6 +6,11 @@
<script> <script>
export default { export default {
name: 'zan-swipe-item' name: 'zan-swipe-item',
beforeCreate() {
this.$parent.swipes.push(this);
}
}; };
</script> </script>

View File

@ -1,6 +1,14 @@
<template> <template>
<div class="zan-swipe"> <div class="zan-swipe">
<slot></slot> <div class="zan-swipe__items">
<slot></slot>
</div>
<div class="zan-swipe__indicators" v-if="showIndicators">
<span class="zan-swipe__indicator" v-for="i in swipes.length" :class="{
'zan-swipe__indicator--active': currIndex === i -1
}">
</span>
</div>
</div> </div>
</template> </template>
@ -13,13 +21,17 @@ export default {
name: 'zan-swipe', name: 'zan-swipe',
props: { props: {
autoPlay: { autoPlay: Boolean,
showIndicators: {
type: Boolean, type: Boolean,
default: false default: true
}, }
onPageChangeEnd: { },
type: Function,
default: () => {} data() {
return {
currIndex: 0,
swipes: []
} }
}, },
@ -53,6 +65,13 @@ export default {
updated() { updated() {
this.scroll.update(); this.scroll.update();
},
methods: {
onPageChangeEnd(page, currIndex) {
this.currIndex = +currIndex;
this.$emit('pagechange:end', page, currIndex);
}
} }
}; };
</script> </script>

View File

@ -1,24 +1,49 @@
@import './common/var.css';
@component-namespace zan { @component-namespace zan {
@b swipe { @b swipe {
position: relative; position: relative;
overflow: hidden; overflow: hidden;
width: 100%; width: 100%;
@e indicators {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
} }
@b swipe-item { @e indicator {
display: none; width: 5px;
width: 100%; height: 5px;
height: 100%; display: inline-block;
overflow: hidden; border-radius: 100%;
text-align: center; background: #000;
opacity: .2;
margin: 0 3px;
z-index: 1;
img { @m active {
width: 100%; background: $c-orange;
height: auto; opacity: 1;
} }
&:first-child {
display: block;
}
} }
}
@b swipe-item {
display: none;
width: 100%;
height: 100%;
overflow: hidden;
text-align: center;
img {
width: 100%;
height: auto;
}
&:first-child {
display: block;
}
}
} }