From 8e1d8329aa7c2c1088f71e95bd1abd2a352adbb1 Mon Sep 17 00:00:00 2001 From: devange Date: Tue, 11 Apr 2017 13:44:54 +0800 Subject: [PATCH] Slider: add vertical mode --- examples/docs/en-US/slider.md | 33 +++++++++++- examples/docs/zh-CN/slider.md | 33 +++++++++++- packages/slider/src/button.vue | 24 +++++++-- packages/slider/src/main.vue | 41 +++++++++++---- packages/theme-default/src/slider.css | 75 +++++++++++++++++++++++++++ 5 files changed, 189 insertions(+), 17 deletions(-) diff --git a/examples/docs/en-US/slider.md b/examples/docs/en-US/slider.md index 1d3c29cd2..e151c4116 100644 --- a/examples/docs/en-US/slider.md +++ b/examples/docs/en-US/slider.md @@ -10,7 +10,8 @@ value6: 0, value7: 0, value8: 0, - value9: [4, 8] + value9: [4, 8], + value10: 0 }; }, methods: { @@ -171,6 +172,34 @@ Selecting a range of values is supported. ``` ::: +### Vertical mode + +Vertical slider + +:::demo Setting the `vertical` attribute to switch to the vertical mode, the `size` attribute must be setted as the heigth of slider +```html + + + +``` +::: + ## Attributes | Attribute | Description | Type | Accepted Values | Default | |---------- |-------------- |---------- |-------------------------------- |-------- | @@ -184,6 +213,8 @@ Selecting a range of values is supported. | show-tooltip | whether to display tooltip value | boolean | — | true | | format-tooltip | format to display tooltip value | Function(value) | — | — | | range | whether to select a range | boolean | — | false | +| vertical | vertical mode | boolean | — | false | +| size | width or height, it should be setted when vertical mode | String | — | - | ## Events | Event Name | Description | Parameters | diff --git a/examples/docs/zh-CN/slider.md b/examples/docs/zh-CN/slider.md index 99af558b5..e050fed8c 100644 --- a/examples/docs/zh-CN/slider.md +++ b/examples/docs/zh-CN/slider.md @@ -10,7 +10,8 @@ value6: 0, value7: 0, value8: 0, - value9: [4, 8] + value9: [4, 8], + value10: 0 }; }, methods: { @@ -195,6 +196,34 @@ ``` ::: +### 竖向模式 + +竖向滑块 + +:::demo 设置`vertical`可使滑块变成竖向模式,必须设置滑块高度`size`属性 +```html + + + +``` +::: + ### Attributes | 参数 | 说明 | 类型 | 可选值 | 默认值 | |---------- |-------------- |---------- |-------------------------------- |-------- | @@ -208,6 +237,8 @@ | show-tooltip | 是否显示 tooltip | boolean | — | true | | format-tooltip | 格式化 tooltip message | Function(value) | — | — | | range | 是否为范围选择 | boolean | — | false | +| vertical | 是否竖向模式 | boolean | — | false | +| size | 宽度或高度,竖向模式时务必设置 | String | — | - | ### Events | 事件名称 | 说明 | 回调参数 | diff --git a/packages/slider/src/button.vue b/packages/slider/src/button.vue index 8bc54653f..e4322dc21 100644 --- a/packages/slider/src/button.vue +++ b/packages/slider/src/button.vue @@ -5,7 +5,7 @@ @mouseleave="handleMouseLeave" @mousedown="onButtonDown" :class="{ 'hover': hovering, 'dragging': dragging }" - :style="{ left: currentPosition }" + :style="this.vertical ? { bottom: currentPosition } : { left: currentPosition }" ref="button"> {{ formatValue }} @@ -28,6 +28,10 @@ value: { type: Number, default: 0 + }, + vertical: { + type: Boolean, + default: false } }, @@ -37,6 +41,8 @@ dragging: false, startX: 0, currentX: 0, + startY: 0, + currentY: 0, startPosition: 0, newPosition: null, oldValue: this.value @@ -117,15 +123,25 @@ onDragStart(event) { this.dragging = true; - this.startX = event.clientX; + if (this.vertical) { + this.startY = event.clientY; + } else { + this.startX = event.clientX; + } this.startPosition = parseFloat(this.currentPosition); }, onDragging(event) { if (this.dragging) { this.displayTooltip(); - this.currentX = event.clientX; - const diff = (this.currentX - this.startX) / this.$parent.$sliderWidth * 100; + let diff = 0; + if (this.vertical) { + this.currentY = event.clientY; + diff = (this.startY - this.currentY) / this.$parent.$sliderSize * 100; + } else { + this.currentX = event.clientX; + diff = (this.currentX - this.startX) / this.$parent.$sliderSize * 100; + } this.newPosition = this.startPosition + diff; this.setPosition(this.newPosition); } diff --git a/packages/slider/src/main.vue b/packages/slider/src/main.vue index f3dbec242..50f64df67 100644 --- a/packages/slider/src/main.vue +++ b/packages/slider/src/main.vue @@ -1,5 +1,6 @@