slider - 纵向区间

This commit is contained in:
halo 2021-12-28 13:09:08 +08:00
parent c66b00f066
commit 79b417947d
4 changed files with 95 additions and 36 deletions

View File

@ -1,3 +1,5 @@
{ {
"semi": true,
"singleQuote": false,
"tabWidth": 2
} }

View File

@ -69,5 +69,27 @@ export default {
</script> </script>
::: :::
::: title 纵向区间
:::
::: demo
<template>
<lay-slider v-model="value3" :range="true" :vertical="true"></lay-slider>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const value4 = ref([20,50])
return {
value4
}
}
}
</script>
:::
::: comment ::: comment
::: :::

View File

@ -99,3 +99,10 @@
position: relative; position: relative;
cursor: pointer; cursor: pointer;
} }
.layui-slider-vrange {
width: 100%;
height: 100%;
position: relative;
cursor: pointer;
}

View File

@ -1,6 +1,11 @@
<template> <template>
<div class="layui-slider-vertical" v-if="vertical"> <div class="layui-slider-vertical" v-if="vertical">
<div v-if="range">range vertical slider</div> <div ref="rangetracker2" class="layui-slider-vrange" v-if="range">
<div class="layui-slider-vertical-btn"></div>
<div class="layui-slider-vertical-btn"></div>
<div class="layui-slider-vertical-line"></div>
<div class="layui-slider-vertical-rate"></div>
</div>
<div <div
ref="verticaltracker" ref="verticaltracker"
@ -26,27 +31,35 @@
</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"
<div @mousedown.stop="handle_mousedown" ref="rangetracker1" class="layui-slider-srange" v-if="range"> >
<lay-tooltip :content="rangeValue[0] + ''"> <lay-tooltip :content="rangeValue[0] + ''">
<div :style="{ left: rangeValue[0] + '%' }" class="layui-slider-btn-v"></div> <div
:style="{ left: rangeValue[0] + '%' }"
class="layui-slider-btn-v"
></div>
</lay-tooltip> </lay-tooltip>
<lay-tooltip :content="rangeValue[1]"> <lay-tooltip :content="rangeValue[1]">
<div :style="{ left: rangeValue[1] + '%' }" class="layui-slider-btn-v"></div> <div
:style="{ left: rangeValue[1] + '%' }"
class="layui-slider-btn-v"
></div>
</lay-tooltip> </lay-tooltip>
<div class="layui-slider-line-v"></div> <div class="layui-slider-line-v"></div>
<div :style="{ width:rangeValue[1]-rangeValue[0] +'%', left: rangeValue[0]+'%' }" class="layui-slider-rate-v"></div> <div
:style="{
width: rangeValue[1] - rangeValue[0] + '%',
left: rangeValue[0] + '%',
}"
class="layui-slider-rate-v"
></div>
</div> </div>
<div <div
ref="standardtracker" ref="standardtracker"
@mousedown.stop="handle_mousedown" @mousedown.stop="handle_mousedown"
@ -71,7 +84,7 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { defineProps, onMounted, reactive, Ref, ref } from "vue"; import { defineProps, reactive, Ref, ref } from "vue";
import { on, off } from "evtd"; import { on, off } from "evtd";
import "./index.less"; import "./index.less";
@ -93,13 +106,23 @@ const props = withDefaults(defineProps<LaySliderProps>(), {
disabled: false, disabled: false,
}); });
let rangeValue:Ref<number[]> = ref([0,0]) let rangeValue: Ref<number[]> = ref([0, 0]);
if(Array.isArray(props.modelValue)){ if (Array.isArray(props.modelValue)) {
rangeValue.value = props.modelValue // eslint-disable-next-line vue/no-setup-props-destructure
rangeValue.value = props.modelValue;
} }
let verticalRangeValue: Ref<number[]> = ref([0, 0]);
if (Array.isArray(props.modelValue)) {
// eslint-disable-next-line vue/no-setup-props-destructure
verticalRangeValue.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 rangetracker1 = ref<HTMLElement | null>(null);
const rangetracker2 = ref<HTMLElement | null>(null);
const standard_style = reactive({ const standard_style = reactive({
left: props.modelValue, left: props.modelValue,
width: props.modelValue, width: props.modelValue,
@ -131,6 +154,10 @@ function handle_mousemove(e: MouseEvent) {
if (props.vertical === false && props.range === true) { if (props.vertical === false && props.range === true) {
starndardRangeMove(e); starndardRangeMove(e);
} }
if (props.vertical === true && props.range === true) {
verticalRangeMove(e);
}
} }
function handle_mouseup() { function handle_mouseup() {
@ -188,7 +215,7 @@ const verticalMove = (e: MouseEvent) => {
emit("update:modelValue", vertical_style.bottom); emit("update:modelValue", vertical_style.bottom);
}; };
const starndardRangeMove =(e:MouseEvent)=>{ const starndardRangeMove = (e: MouseEvent) => {
if (!rangetracker1.value) { if (!rangetracker1.value) {
return; return;
} }
@ -196,31 +223,32 @@ const starndardRangeMove =(e:MouseEvent)=>{
let origin_left = tracker_rect.left; let origin_left = tracker_rect.left;
let point_left = e.clientX; let point_left = e.clientX;
let distance = point_left - origin_left; let distance = point_left - origin_left;
if(distance < 0){ if (distance < 0) {
rangeValue.value[0] = 0 rangeValue.value[0] = 0;
} else { } else {
let rate = (distance / tracker_rect.width) * 100; let rate = (distance / tracker_rect.width) * 100;
let idx = moveNeighbor(Math.floor(rate)) let idx = moveNeighbor(Math.floor(rate));
rangeValue.value[idx] = Math.floor(rate) rangeValue.value[idx] = Math.floor(rate);
if(rangeValue.value[1] > 100) { if (rangeValue.value[1] > 100) {
rangeValue.value[1] = 100 rangeValue.value[1] = 100;
} }
if(rangeValue.value[0] < 0) { if (rangeValue.value[0] < 0) {
rangeValue.value[0] = 0 rangeValue.value[0] = 0;
} }
} }
emit("update:modelValue", rangeValue.value); emit("update:modelValue", rangeValue.value);
} };
const verticalRangeMove = (e: MouseEvent) => {};
function moveNeighbor(rate: number) { function moveNeighbor(rate: number) {
let d1 = Math.abs(rate - rangeValue.value[0]); let d1 = Math.abs(rate - rangeValue.value[0]);
let d2 = Math.abs(rate - rangeValue.value[1]); let d2 = Math.abs(rate - rangeValue.value[1]);
if(d1 > d2){ if (d1 > d2) {
return 1 return 1;
} else { } else {
return 0 return 0;
} }
} }
</script> </script>