slider - 区间形式

This commit is contained in:
halo 2021-12-26 21:40:02 +08:00
parent e4bfc18c9d
commit 1d5ca9cae6
3 changed files with 97 additions and 30 deletions

View File

@ -1,3 +1,6 @@
::: title 横向
:::
::: demo ::: demo
<template> <template>
@ -19,10 +22,12 @@ export default {
::: :::
::: title 纵向
:::
::: demo ::: demo
<template> <template>
<lay-slider v-model="value2" :vertical="vertical" :disabled="true"></lay-slider> <lay-slider v-model="value2" :vertical="true" :disabled="true"></lay-slider>
</template> </template>
<script> <script>
@ -31,11 +36,9 @@ import { ref } from 'vue'
export default { export default {
setup() { setup() {
const vertical = ref(true)
const value2 = ref(10) const value2 = ref(10)
return { return {
vertical,
value2 value2
} }
} }
@ -44,23 +47,27 @@ export default {
::: :::
::: title 区间
:::
::: demo ::: demo
<tempalte> <template>
<lay-slider v-model="value3" :range="true"></lay-slider> <lay-slider v-model="value3" :range="true"></lay-slider>
</template> </template>
<script> <script>
import { ref } from 'vue' import { ref } from 'vue'
export default { export default {
setup() { setup() {
const value3 = ref(50) const value3 = ref([20,50])
return { return {
value1 value3
} }
} }
} }
</script> </script>
:::
::: comment
::: :::

View File

@ -18,24 +18,12 @@
height: 12px; height: 12px;
background-color: white; background-color: white;
position: absolute; position: absolute;
// top: -7px;
border: 2px solid rgb(0, 150, 136); border: 2px solid rgb(0, 150, 136);
border-radius: 50%; border-radius: 50%;
cursor: pointer; cursor: pointer;
left: 0%; left: 0%;
z-index: 2; z-index: 2;
} }
// .layui-slider-btn-v:hover {
// width: 14px;
// height: 14px;
// background-color: white;
// position: absolute;
// top: -2px;
// border: 2px solid rgb(0, 150, 136);
// border-radius: 50%;
// cursor: pointer;
// left: 0%;
// }
.layui-slider-rate-v { .layui-slider-rate-v {
width: 0%; width: 0%;
@ -65,13 +53,11 @@
.layui-slider-vertical { .layui-slider-vertical {
width: 18px; width: 18px;
height: 200px; height: 200px;
// background-color: red;
} }
.layui-slider-vertical-track { .layui-slider-vertical-track {
width: 100%; width: 100%;
height: 100%; height: 100%;
// background-color: #eee;
position: relative; position: relative;
} }
@ -106,3 +92,10 @@
left: 7px; left: 7px;
background-color: #eee; background-color: #eee;
} }
.layui-slider-srange {
width: 100%;
height: 100%;
position: relative;
cursor: pointer;
}

View File

@ -1,13 +1,13 @@
<template> <template>
<div class="layui-slider-vertical" v-if="vertical"> <div class="layui-slider-vertical" v-if="vertical">
<div v-if="range">rang div</div> <div v-if="range">range vertical slider</div>
<div <div
v-else
ref="verticaltracker" ref="verticaltracker"
@mousedown.stop="handle_mousedown" @mousedown.stop="handle_mousedown"
class="layui-slider-vertical-track" class="layui-slider-vertical-track"
:class="[props.disabled ? 'layui-slider-disabled' : '']" :class="[props.disabled ? 'layui-slider-disabled' : '']"
v-else
> >
<lay-tooltip :content="modelValue + ''"> <lay-tooltip :content="modelValue + ''">
<div <div
@ -26,11 +26,33 @@
</div> </div>
<div class="layui-slider-v" v-else> <div class="layui-slider-v" v-else>
<div @mousedown.stop="handle_mousedown" ref="rangetracker1" class="layui-slider-srange" v-if="range">
<lay-tooltip :content="rangeValue[0] + ''">
<div :style="{ left: rangeValue[0] + '%' }" class="layui-slider-btn-v"></div>
</lay-tooltip>
<lay-tooltip :content="rangeValue[1]">
<div :style="{ left: rangeValue[1] + '%' }" class="layui-slider-btn-v"></div>
</lay-tooltip>
<div class="layui-slider-line-v"></div>
<div :style="{ width:rangeValue[1]-rangeValue[0] +'%', left: rangeValue[0]+'%' }" class="layui-slider-rate-v"></div>
</div>
<div <div
ref="standardtracker" ref="standardtracker"
@mousedown.stop="handle_mousedown" @mousedown.stop="handle_mousedown"
class="layui-slider-track-v" class="layui-slider-track-v"
:class="[props.disabled ? 'layui-slider-disabled' : '']" :class="[props.disabled ? 'layui-slider-disabled' : '']"
v-else
> >
<lay-tooltip :content="modelValue + ''"> <lay-tooltip :content="modelValue + ''">
<div <div
@ -49,7 +71,7 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { defineProps, reactive, ref } from "vue"; import { defineProps, onMounted, reactive, Ref, ref } from "vue";
import { on, off } from "evtd"; import { on, off } from "evtd";
import "./index.less"; import "./index.less";
@ -57,7 +79,7 @@ const emit = defineEmits(["update:modelValue"]);
interface LaySliderProps { interface LaySliderProps {
vertical?: boolean; vertical?: boolean;
modelValue?: number; modelValue?: number | Array<number>;
min?: number; min?: number;
max?: number; max?: number;
step?: number; step?: number;
@ -71,8 +93,13 @@ const props = withDefaults(defineProps<LaySliderProps>(), {
disabled: false, disabled: false,
}); });
let rangeValue:Ref<number[]> = ref([0,0])
if(Array.isArray(props.modelValue)){
rangeValue.value = props.modelValue
}
const standardtracker = ref<HTMLElement | null>(null); const standardtracker = ref<HTMLElement | null>(null);
const verticaltracker = ref<HTMLElement | null>(null); const verticaltracker = ref<HTMLElement | null>(null);
const rangetracker1 = ref<HTMLElement | null>(null);
const standard_style = reactive({ const standard_style = reactive({
left: props.modelValue, left: props.modelValue,
width: props.modelValue, width: props.modelValue,
@ -93,11 +120,17 @@ function handle_mousemove(e: MouseEvent) {
if (props.disabled === true) { if (props.disabled === true) {
return false; return false;
} }
if (props.vertical === false) { if (props.vertical === false && props.range === false) {
standardMove(e); standardMove(e);
} else { }
if (props.vertical === true && props.range === false) {
verticalMove(e); verticalMove(e);
} }
if (props.vertical === false && props.range === true) {
starndardRangeMove(e);
}
} }
function handle_mouseup() { function handle_mouseup() {
@ -128,7 +161,6 @@ const standardMove = (e: MouseEvent) => {
if (standard_style.left > 100) { if (standard_style.left > 100) {
standard_style.left = 100; standard_style.left = 100;
standard_style.width = 100; standard_style.width = 100;
// props.modelValue = 100;
} }
} }
emit("update:modelValue", standard_style.left); emit("update:modelValue", standard_style.left);
@ -151,9 +183,44 @@ const verticalMove = (e: MouseEvent) => {
if (vertical_style.bottom > 100) { if (vertical_style.bottom > 100) {
vertical_style.bottom = 100; vertical_style.bottom = 100;
vertical_style.height = 100; vertical_style.height = 100;
// props.modelValue = 100;
} }
} }
emit("update:modelValue", vertical_style.bottom); emit("update:modelValue", vertical_style.bottom);
}; };
const starndardRangeMove =(e:MouseEvent)=>{
if (!rangetracker1.value) {
return;
}
let tracker_rect = rangetracker1.value.getBoundingClientRect();
let origin_left = tracker_rect.left;
let point_left = e.clientX;
let distance = point_left - origin_left;
if(distance < 0){
rangeValue.value[0] = 0
} else {
let rate = (distance / tracker_rect.width) * 100;
let idx = moveNeighbor(Math.floor(rate))
rangeValue.value[idx] = Math.floor(rate)
if(rangeValue.value[1] > 100) {
rangeValue.value[1] = 100
}
if(rangeValue.value[0] < 0) {
rangeValue.value[0] = 0
}
}
emit("update:modelValue", rangeValue.value);
}
function moveNeighbor(rate: number) {
let d1 = Math.abs(rate - rangeValue.value[0]);
let d2 = Math.abs(rate - rangeValue.value[1]);
if(d1 > d2){
return 1
} else {
return 0
}
}
</script> </script>