[new feature] Slider: add button slot & drag event (#1148)

This commit is contained in:
neverland 2018-12-25 11:52:15 +08:00 committed by GitHub
parent 7b61eeaf33
commit 34a048e134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 104 additions and 17 deletions

View File

@ -1,10 +1,20 @@
import Page from '../../common/page'; import Page from '../../common/page';
Page({ Page({
data: {
currentValue: 30
},
onChange(event) { onChange(event) {
wx.showToast({ wx.showToast({
icon: 'none', icon: 'none',
title: `当前值:${event.detail}` title: `当前值:${event.detail}`
}); });
},
onDrag(event) {
this.setData({
currentValue: event.detail.value
});
} }
}); });

View File

@ -1,5 +1,9 @@
<demo-block title="基础用法"> <demo-block title="基础用法">
<van-slider custom-class="slider" value="50" bind:change="onChange" /> <van-slider
value="50"
custom-class="slider"
bind:change="onChange"
/>
</demo-block> </demo-block>
<demo-block title="指定选择范围"> <demo-block title="指定选择范围">
@ -13,7 +17,11 @@
</demo-block> </demo-block>
<demo-block title="禁用"> <demo-block title="禁用">
<van-slider custom-class="slider" value="50" disabled /> <van-slider
custom-class="slider"
value="50"
disabled
/>
</demo-block> </demo-block>
<demo-block title="指定步长"> <demo-block title="指定步长">
@ -25,3 +33,16 @@
bind:change="onChange" bind:change="onChange"
/> />
</demo-block> </demo-block>
<demo-block title="自定义按钮">
<van-slider
value="{{ currentValue }}"
custom-class="slider"
use-button-slot
bind:drag="onDrag"
>
<view class="custom-button" slot="button">
{{ currentValue }}/100
</view>
</van-slider>
</demo-block>

View File

@ -1,3 +1,13 @@
.slider { .slider {
margin: 0 15px 30px; margin: 0 15px 30px;
} }
.custom-button {
width: 44px;
color: #fff;
font-size: 10px;
line-height: 20px;
text-align: center;
border-radius: 100px;
background-color: #f44;
}

View File

@ -42,6 +42,34 @@ Page({
<van-slider value="50" step="10" bar-height="4px" /> <van-slider value="50" step="10" bar-height="4px" />
``` ```
#### 自定义按钮
```html
<van-slider
value="{{ currentValue }}"
use-button-slot
bind:drag="onDrag"
>
<view class="custom-button" slot="button">
{{ currentValue }}/100
</view>
</van-slider>
```
```js
Page({
data: {
currentValue: 30
},
onDrag(event) {
this.setData({
currentValue: event.detail.value
});
}
});
```
### API ### API
| 参数 | 说明 | 类型 | 默认值 | | 参数 | 说明 | 类型 | 默认值 |
@ -57,6 +85,7 @@ Page({
| 事件名 | 说明 | 参数 | | 事件名 | 说明 | 参数 |
|-----------|-----------|-----------| |-----------|-----------|-----------|
| bind:drag | 拖动进度条时触发 | event.detail.value: 当前进度 |
| bind:change | 进度值改变后触发 | event.detail: 当前进度 | | bind:change | 进度值改变后触发 | event.detail: 当前进度 |
### 外部样式类 ### 外部样式类

View File

@ -12,24 +12,27 @@
} }
&__button { &__button {
position: absolute;
top: 50%;
right: 0;
width: 20px; width: 20px;
height: 20px; height: 20px;
border-radius: 50%; border-radius: 50%;
background-color: @white; background-color: @white;
transform: translate3d(50%, -50%, 0);
box-shadow: 0 1px 2px rgba(0, 0, 0, .5); box-shadow: 0 1px 2px rgba(0, 0, 0, .5);
/* use pseudo element to expand touch area */ &-wrapper {
&::after {
content: '';
position: absolute; position: absolute;
width: 200%; top: 50%;
height: 200%; right: 0;
top: -50%; transform: translate3d(50%, -50%, 0);
left: -50%;
/* use pseudo element to expand touch area */
&::after {
content: '';
position: absolute;
width: 200%;
height: 200%;
top: -50%;
left: -50%;
}
} }
} }

View File

@ -6,6 +6,7 @@ VantComponent({
props: { props: {
disabled: Boolean, disabled: Boolean,
useButtonSlot: Boolean,
max: { max: {
type: Number, type: Number,
value: 100 value: 100
@ -78,6 +79,7 @@ VantComponent({
barStyle: `width: ${value}%; height: ${this.data.barHeight};` barStyle: `width: ${value}%; height: ${this.data.barHeight};`
}); });
this.$emit('drag', { value });
if (end) { if (end) {
this.$emit('change', value); this.$emit('change', value);
} }

View File

@ -1,11 +1,23 @@
<view class="custom-class van-slider {{ disabled ? 'van-slider--disabled' : '' }}" bind:tap="onClick"> <view
class="custom-class van-slider {{ disabled ? 'van-slider--disabled' : '' }}"
bind:tap="onClick"
>
<view class="van-slider__bar" style="{{ barStyle }}"> <view class="van-slider__bar" style="{{ barStyle }}">
<view <view
class="van-slider__button" class="van-slider__button-wrapper"
bind:touchstart="onTouchStart" bind:touchstart="onTouchStart"
catch:touchmove="onTouchMove" catch:touchmove="onTouchMove"
bind:touchend="onTouchEnd" bind:touchend="onTouchEnd"
bind:touchcancel="onTouchEnd" bind:touchcancel="onTouchEnd"
/> >
<slot
wx:if="{{ useButtonSlot }}"
name="button"
/>
<view
wx:else
class="van-slider__button"
/>
</view>
</view> </view>
</view> </view>