feat(slider - 增加断点 showdots): slider - 增加断点

This commit is contained in:
halo 2022-05-03 22:44:29 +08:00
parent 436fd407ca
commit ba7c0eeae9
8 changed files with 134 additions and 9 deletions

View File

@ -13,7 +13,7 @@
::: demo 使用 `lay-slider` 标签, 创建滑块 ::: demo 使用 `lay-slider` 标签, 创建滑块
<template> <template>
<lay-slider :max="88" v-model="value1" :disabled="false"></lay-slider> <lay-slider :showDots="true" :step="10" :max="100" v-model="value1" :disabled="false"></lay-slider>
<lay-input-number v-model="value1"></lay-input-number> <lay-input-number v-model="value1"></lay-input-number>
</template> </template>
@ -37,7 +37,7 @@ export default {
::: demo ::: demo
<template> <template>
<lay-slider v-model="value2" :vertical="true" :disabled="false"></lay-slider> <lay-slider :showDots="false" :step="10" v-model="value2" :vertical="true" :disabled="false"></lay-slider>
</template> </template>
<script> <script>
@ -63,7 +63,7 @@ export default {
::: demo ::: demo
<template> <template>
<lay-slider :disabled="false" :min="10" :max="80" v-model:rangeValue="value3" :range="true"></lay-slider> <lay-slider :disabled="false" :min="0" :max="100" v-model:rangeValue="value3" :range="true"></lay-slider>
</template> </template>
<script> <script>
@ -115,6 +115,7 @@ export default {
| step | 步长 | `Number` | - | - | | step | 步长 | `Number` | - | - |
| min | 最小值 | `Number` | - | - | | min | 最小值 | `Number` | - | - |
| max | 最大值 | `Number` | - | - | | max | 最大值 | `Number` | - | - |
| showDots | 是否显示断点 | `Boolean` | - | false |
::: :::

View File

@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { Ref, ref } from "vue"; import { Ref, ref } from "vue";
import { on, off } from "evtd"; import { on, off } from "evtd";
import { throttle, handle_select } from "./utils/index"; import { throttle, handle_select, makeDots } from "./utils/index";
interface Prop { interface Prop {
val?: number | Array<number>; val?: number | Array<number>;
@ -9,6 +9,7 @@ interface Prop {
step?: number; step?: number;
min?: number; min?: number;
max?: number; max?: number;
showDots?: boolean;
} }
const props = withDefaults(defineProps<Prop>(), { const props = withDefaults(defineProps<Prop>(), {
disabled: false, disabled: false,
@ -16,6 +17,7 @@ const props = withDefaults(defineProps<Prop>(), {
step: 0, step: 0,
min: 0, min: 0,
max: 100, max: 100,
showDots: false,
}); });
const moveAction = throttle(standardMove); const moveAction = throttle(standardMove);
@ -80,6 +82,15 @@ function calcWithStep(
} }
} }
} }
//
const dots = makeDots(props);
const focusDot = (val: number) => {
emit("link-val-hook", val);
};
// const focusClick = (e: MouseEvent)=>{
// console.log(e);
// standardMove(e)
// }
</script> </script>
<template> <template>
@ -103,5 +114,13 @@ function calcWithStep(
:class="[disabled ? 'layui-slider-disabled disable-line' : '']" :class="[disabled ? 'layui-slider-disabled disable-line' : '']"
></div> ></div>
<div class="layui-slider-line-v"></div> <div class="layui-slider-line-v"></div>
<div
v-show="showDots"
@click="focusDot(item)"
class="layui-slider-dots"
v-for="(item, index) in dots"
:key="index"
:style="{ left: item + '%' }"
></div>
</div> </div>
</template> </template>

View File

@ -1,13 +1,14 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, toRef, Ref } from "vue"; import { ref, toRef, Ref } from "vue";
import { on, off } from "evtd"; import { on, off } from "evtd";
import { throttle } from "./utils/index"; import { throttle, makeDots } from "./utils/index";
interface Prop { interface Prop {
rangeValue: Array<number>; rangeValue: Array<number>;
disabled?: boolean; disabled?: boolean;
step?: number; step?: number;
min?: number; min?: number;
max?: number; max?: number;
showDots?: boolean;
} }
const props = withDefaults(defineProps<Prop>(), { const props = withDefaults(defineProps<Prop>(), {
@ -15,6 +16,7 @@ const props = withDefaults(defineProps<Prop>(), {
min: 0, min: 0,
max: 100, max: 100,
disabled: false, disabled: false,
showDots: true,
}); });
let rv = toRef(props, "rangeValue"); let rv = toRef(props, "rangeValue");
@ -120,6 +122,15 @@ function cross(val: any) {
currbtn = currbtn === 0 ? 1 : 0; currbtn = currbtn === 0 ? 1 : 0;
} }
} }
//
const dots = makeDots(props);
console.log(dots);
const focusDot = (item: number) => {
let currbtn = moveNeighbors(item, rv);
rv.value[currbtn] = item;
emit("link-val-hook", rv.value);
};
</script> </script>
<template> <template>
@ -153,5 +164,13 @@ function cross(val: any) {
class="layui-slider-rate-v" class="layui-slider-rate-v"
:class="[props.disabled ? 'layui-slider-disabled disable-line' : '']" :class="[props.disabled ? 'layui-slider-disabled disable-line' : '']"
></div> ></div>
<div
v-show="showDots"
@click="focusDot(item)"
class="layui-slider-dots"
v-for="(item, index) in dots"
:key="index"
:style="{ left: item + '%' }"
></div>
</div> </div>
</template> </template>

View File

@ -9,6 +9,7 @@ interface Prop {
step?: number; step?: number;
min?: number; min?: number;
max?: number; max?: number;
showDots: boolean;
} }
const props = withDefaults(defineProps<Prop>(), { const props = withDefaults(defineProps<Prop>(), {
disabled: true, disabled: true,
@ -16,6 +17,7 @@ const props = withDefaults(defineProps<Prop>(), {
step: 0, step: 0,
min: 0, min: 0,
max: 100, max: 100,
showDots: false,
}); });
const moveAction = throttle(verticalMove); const moveAction = throttle(verticalMove);
@ -83,6 +85,22 @@ function calcWithStep(
} }
} }
} }
//
const makeDots = () => {
if (props.step === 0) return [];
let val = 0;
let dots = [];
let count = Math.floor(100 / props.step) - 1;
for (let i = 0; i < count; i++) {
val += props.step;
dots.push(val);
}
return dots;
};
const dots = makeDots();
const focusDot = (val: number) => {
emit("link-val-hook", val);
};
</script> </script>
<template> <template>
@ -107,6 +125,14 @@ function calcWithStep(
class="layui-slider-vertical-rate" class="layui-slider-vertical-rate"
></div> ></div>
<div class="layui-slider-vertical-line"></div> <div class="layui-slider-vertical-line"></div>
<div
v-show="showDots"
@click="focusDot(item)"
class="layui-slider-vertical-dots"
v-for="(item, index) in dots"
:key="index"
:style="{ bottom: item + '%' }"
></div>
</div> </div>
</div> </div>
</template> </template>

View File

@ -1,13 +1,14 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, toRef, Ref } from "vue"; import { ref, toRef, Ref } from "vue";
import { on, off } from "evtd"; import { on, off } from "evtd";
import { throttle } from "./utils/index"; import { throttle, makeDots } from "./utils/index";
interface Prop { interface Prop {
rangeValue: Array<number>; rangeValue: Array<number>;
disabled?: boolean; disabled?: boolean;
step?: number; step?: number;
min?: number; min?: number;
max?: number; max?: number;
showDots?: boolean;
} }
const props = withDefaults(defineProps<Prop>(), { const props = withDefaults(defineProps<Prop>(), {
@ -15,6 +16,7 @@ const props = withDefaults(defineProps<Prop>(), {
min: 0, min: 0,
max: 100, max: 100,
disabled: false, disabled: false,
showDots: false,
}); });
let rv = toRef(props, "rangeValue"); let rv = toRef(props, "rangeValue");
@ -120,6 +122,15 @@ function cross(val: any) {
currbtn = currbtn === 0 ? 1 : 0; currbtn = currbtn === 0 ? 1 : 0;
} }
} }
//
const dots = makeDots(props);
console.log(dots);
const focusDot = (item: number) => {
let currbtn = moveNeighbors(item, rv);
rv.value[currbtn] = item;
emit("link-val-hook", rv.value);
};
</script> </script>
<template> <template>
@ -155,6 +166,14 @@ function cross(val: any) {
class="layui-slider-vertical-rate" class="layui-slider-vertical-rate"
:class="[props.disabled ? 'layui-slider-disabled disable-line' : '']" :class="[props.disabled ? 'layui-slider-disabled disable-line' : '']"
></div> ></div>
<div
v-show="showDots"
@click="focusDot(item)"
class="layui-slider-vertical-dots"
v-for="(item, index) in dots"
:key="index"
:style="{ bottom: item + '%' }"
></div>
</div> </div>
</div> </div>
</template> </template>

View File

@ -18,7 +18,8 @@
.layui-slider-btn-v { .layui-slider-btn-v {
width: 12px; width: 12px;
height: 12px; height: 12px;
background-color: @global-back-color; // background-color: @global-back-color;
background-color: white;
position: absolute; position: absolute;
border: 2px solid @global-primary-color; border: 2px solid @global-primary-color;
border-radius: 50%; border-radius: 50%;
@ -40,7 +41,7 @@
.layui-slider-line-v { .layui-slider-line-v {
width: 100%; width: 100%;
height: 4px; height: 4px;
background-color: #eee; background-color: #cccccc;
position: absolute; position: absolute;
top: 6px; top: 6px;
} }
@ -74,7 +75,8 @@
.layui-slider-vertical-btn { .layui-slider-vertical-btn {
width: 12px; width: 12px;
height: 12px; height: 12px;
background-color: @global-back-color; // background-color: @global-back-color;
background-color: #ffffff;
position: absolute; position: absolute;
border: 2px solid @global-primary-color; border: 2px solid @global-primary-color;
border-radius: 50%; border-radius: 50%;
@ -115,3 +117,23 @@
position: relative; position: relative;
cursor: pointer; cursor: pointer;
} }
.layui-slider-dots {
margin-top: 4px;
width: 8px;
height: 8px;
background-color: #ffffff;
border-radius: 5px;
position: absolute;
top:0;
z-index: 1;
}
.layui-slider-vertical-dots {
width: 8px;
height: 8px;
background-color: #ffffff;
border-radius: 5px;
position: absolute;
z-index: 1;
margin-left: 5px;
}

View File

@ -15,6 +15,7 @@ export interface LaySliderProps {
disabled?: boolean; disabled?: boolean;
range?: boolean; range?: boolean;
rangeValue?: number[]; rangeValue?: number[];
showDots?: boolean
} }
const emit = defineEmits(["update:modelValue"]); const emit = defineEmits(["update:modelValue"]);
@ -26,6 +27,7 @@ const props = withDefaults(defineProps<LaySliderProps>(), {
step: 0, step: 0,
min: 0, min: 0,
max: 100, max: 100,
showDots: false
}); });
let rangeValues: Ref<number[]> | any = toRef(props, "rangeValue"); let rangeValues: Ref<number[]> | any = toRef(props, "rangeValue");
@ -47,6 +49,7 @@ function valHook(val: any) {
:rangeValue="rangeValues" :rangeValue="rangeValues"
:min="min" :min="min"
:max="max" :max="max"
:showDots="showDots"
/> />
</div> </div>
<div v-else> <div v-else>
@ -58,6 +61,7 @@ function valHook(val: any) {
:val="modelValue" :val="modelValue"
:min="min" :min="min"
:max="max" :max="max"
:showDots="showDots"
/> />
</div> </div>
</div> </div>
@ -71,6 +75,7 @@ function valHook(val: any) {
:rangeValue="rangeValues" :rangeValue="rangeValues"
:min="min" :min="min"
:max="max" :max="max"
:showDots="showDots"
/> />
</div> </div>
<div v-else> <div v-else>
@ -82,6 +87,7 @@ function valHook(val: any) {
:step="step" :step="step"
:min="min" :min="min"
:max="max" :max="max"
:showDots="showDots"
></StandardVue> ></StandardVue>
</div> </div>
</div> </div>

View File

@ -13,3 +13,16 @@ export function throttle(func: Function) {
export function handle_select(e: Event): void { export function handle_select(e: Event): void {
e.preventDefault(); e.preventDefault();
} }
export function makeDots(props: any) {
if (props.step === 0) return [];
let val = 0;
let dots = [0];
let count = Math.floor(100 / props.step) - 1;
for (let i = 0; i < count; i++) {
val += props.step;
dots.push(val);
}
dots.push(100);
return dots;
}