♻️(component): 使用 setup script 重构 scroll 组件

This commit is contained in:
就眠儀式 2022-04-30 03:44:28 +08:00
parent 844f3fd0cf
commit 3b1849fb70
2 changed files with 202 additions and 215 deletions

View File

@ -1,44 +1,34 @@
.scrollbar-box {
.layui-scroll {
height: 100%;
overflow: hidden !important;
}
.scrollbar-y {
.layui-scroll-y {
position: relative;
height: 100%;
.scroll-wrap {
.layui-scroll-wrap {
height: 100%;
overflow-y: scroll;
scrollbar-width: none; /* firefox */
scrollbar-width: none;
-ms-overflow-style: none;
}
.scrollbar-track {
.layui-scroll-track {
position: absolute;
top: 0;
right: 0;
bottom: 0;
border-radius: 8px;
z-index: 20;
.scrollbar-thumb {
.layui-scroll-thumb {
margin: 0 auto;
border-radius: 6px;
cursor: default;
}
}
}
.scrollbar-y ::-webkit-scrollbar {
.layui-scroll-y ::-webkit-scrollbar {
display: none;
}
//移动端隐藏自定义滚动条
.hide.scrollbar-box .scrollbar-track {
display: none;
}
//移动端显示原生滑块
.hide.scrollbar-box .scrollbar-y ::-webkit-scrollbar {
display: block;
}
.hide.scrollbar-box .scrollbar-y .scroll-wrap {
scrollbar-width: auto;
-ms-overflow-style: scrollbar;
}

View File

@ -1,16 +1,16 @@
<template>
<div
class="scrollbar-box"
:class="{ hide: winWidth < 500 }"
class="layui-scroll"
:class="{ hide: data.winWidth < 500 }"
:style="{ height: height }"
>
<div class="scrollbar-y">
<div ref="scrollRef" class="scroll-wrap" @scroll="onMosewheel">
<div class="layui-scroll-y">
<div ref="scrollRef" class="layui-scroll-wrap" @scroll="onMosewheel">
<slot></slot>
</div>
<div
ref="barRef"
class="scrollbar-track"
class="layui-scroll-track"
:style="{
backgroundColor: heightPre == 1 ? 'rgba(0,0,0,0)' : slotColor,
}"
@ -21,12 +21,12 @@
-->
<div
:style="{
height: barHeight + 'px',
height: data.barHeight + 'px',
width: scrollWidth + 'px',
transform: 'translateY(' + translateY + 'px)',
backgroundColor: heightPre == 1 ? 'rgba(0,0,0,0)' : scrollColor,
transform: 'translateY(' + data.translateY + 'px)',
backgroundColor: data.heightPre == 1 ? 'rgba(0,0,0,0)' : scrollColor,
}"
class="scrollbar-thumb"
class="layui-scroll-thumb"
@mousedown.stop.prevent="moveStart"
></div>
</div>
@ -34,7 +34,13 @@
</div>
</template>
<script>
<script lang="ts">
export default {
name: "LayScroll"
}
</script>
<script lang="ts" setup>
import "./index.less";
import {
defineComponent,
@ -43,193 +49,184 @@ import {
nextTick,
reactive,
onUnmounted,
ref,
} from "vue";
export default defineComponent({
name: "LayScroll",
props: {
height: {
type: String,
default: "100%",
},
slotColor: {
// :slotColor="'red'"
type: String,
default: "rgba(0,0,0,0)",
},
scrollColor: {
// :scrollColor="'red'"
type: String,
default: "#eeeeee",
},
scrollWidth: {
// :scrollWidth="7"
type: Number,
default: 6,
},
},
setup(props, ctx) {
const data = reactive({
scrollRef: null, //
barRef: null, //
translateY: 0, //
heightPre: 0, //
barHeight: 0, //
winWidth: document.body.clientWidth, //
});
let time = null; //
let isMove = false; //
let moveClientY = 0; //
let trackHeight = 0; //
let wrapHeight = 0; //
let wrapContentHeight = 0; //
//
onMounted(() => {
monitorWindow(); //
monitorScrollBar(); //
nextTick(() => {
//dom
calculationLength(); //
});
});
//
onUnmounted(() => {
window.clearInterval(time);
time = null;
});
//
const monitorWindow = function () {
let time; //
window.addEventListener("resize", () => {
data.winWidth = document.body.clientWidth; //
clearTimeout(time);
time = setTimeout(() => {
//500
// console.log("");
initScrollListner();
}, 500);
});
};
export interface LayScrollProps {
height: string;
slotColor: string;
scrollColor: string;
scrollWidth: number;
}
//
const monitorScrollBar = function () {
var monitorUl = data.scrollRef.children[0];
// var monitorDiv= document; // document
let MutationObserver =
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;
let observer = new MutationObserver(function (mutations) {
// console.log("");
initScrollListner();
});
// childList
//
observer.observe(monitorUl, {
attributes: true,
childList: true,
});
};
//
const calculationLength = function () {
// initScrollListner
// dom
// 10
time = setInterval(() => {
//
initScrollListner();
}, 50);
// 500
setTimeout(() => {
window.clearInterval(time);
time = null;
}, 2000);
};
//
const initScrollListner = function () {
let scroll = data.scrollRef;
let bar = data.barRef;
// scroll
if (scroll) {
wrapContentHeight = scroll.scrollHeight;
wrapHeight = scroll.clientHeight;
trackHeight = bar.clientHeight;
// console.log(wrapContentHeight ,wrapHeight);
// / 100 150
data.heightPre = wrapHeight / wrapContentHeight;
//
data.barHeight = data.heightPre * trackHeight;
}
};
//
const onMosewheel = function (e) {
// scrollTop
// offsetHeight
// scrollHeight
// data.translateY
data.translateY = e.target.scrollTop * data.heightPre;
if (data.translateY == 0) {
//
arrive("top");
} else if (
e.target.scrollTop + e.target.offsetHeight ==
e.target.scrollHeight
) {
// + ==
arrive("bottom");
}
};
//
const arrive = function name(tb) {
ctx.emit("arrive", tb);
};
//
const moveStart = function (e) {
isMove = true;
// clientYy
// data.translateY
// moveClientY
moveClientY = e.clientY - data.translateY;
moveTo(); //
moveEnd(); //
};
// thumbscrollTop
const moveTo = function () {
document.onmousemove = (e) => {
//
if (isMove) {
//
if (e.clientY - moveClientY > trackHeight - data.barHeight) {
// translateY
data.translateY = trackHeight - data.barHeight;
} else if (e.clientY - moveClientY < 0) {
// translateY
data.translateY = 0;
} else {
//
data.translateY = e.clientY - moveClientY;
}
//
data.scrollRef.scrollTop = data.translateY / data.heightPre;
}
};
};
//
const moveEnd = function () {
document.onmouseup = (e) => {
if (isMove) {
isMove = false;
}
};
};
return {
...toRefs(data),
onMosewheel,
moveStart,
};
},
const props = withDefaults(defineProps<LayScrollProps>(), {
height: "100%",
slotColor: "rgba(0,0,0,0)",
scrollColor: "#eeeeee",
scrollWidth: 6,
});
const emit = defineEmits(["arrive"]);
const scrollRef = ref<HTMLElement | null>(null);
const barRef = ref<HTMLElement | null>(null);
const data = reactive({
translateY: 0, //
heightPre: 0, //
barHeight: 0, //
winWidth: document.body.clientWidth, //
});
let time = null; //
let isMove = false; //
let moveClientY = 0; //
let trackHeight = 0; //
let wrapHeight = 0; //
let wrapContentHeight = 0; //
//
onMounted(() => {
monitorWindow(); //
monitorScrollBar(); //
nextTick(() => {
//dom
calculationLength(); //
});
});
//
onUnmounted(() => {
window.clearInterval(time);
time = null;
});
//
const monitorWindow = function () {
let time; //
window.addEventListener("resize", () => {
data.winWidth = document.body.clientWidth; //
clearTimeout(time);
time = setTimeout(() => {
//500
// console.log("");
initScrollListner();
}, 500);
});
};
//
const monitorScrollBar = function () {
var monitorUl = scrollRef.value.children[0];
// var monitorDiv= document; // document
let MutationObserver =
window.MutationObserver ||
// @ts-ignore
window.WebKitMutationObserver ||
// @ts-ignore
window.MozMutationObserver;
let observer = new MutationObserver(function (mutations) {
// console.log("");
initScrollListner();
});
// childList
//
observer.observe(monitorUl, {
attributes: true,
childList: true,
});
};
//
const calculationLength = function () {
// initScrollListner
// dom
// 10
time = setInterval(() => {
//
initScrollListner();
}, 50);
// 500
setTimeout(() => {
window.clearInterval(time);
time = null;
}, 2000);
};
//
const initScrollListner = function () {
let scroll = scrollRef.value;
let bar = barRef.value;
// scroll
if (scroll) {
wrapContentHeight = scroll.scrollHeight;
wrapHeight = scroll.clientHeight;
trackHeight = bar.clientHeight;
// console.log(wrapContentHeight ,wrapHeight);
// / 100 150
data.heightPre = wrapHeight / wrapContentHeight;
//
data.barHeight = data.heightPre * trackHeight;
}
};
//
const onMosewheel = function (e) {
// scrollTop
// offsetHeight
// scrollHeight
// data.translateY
data.translateY = e.target.scrollTop * data.heightPre;
if (data.translateY == 0) {
//
arrive("top");
} else if (
e.target.scrollTop + e.target.offsetHeight ==
e.target.scrollHeight
) {
// + ==
arrive("bottom");
}
};
//
const arrive = function name(tb) {
emit("arrive", tb);
};
//
const moveStart = function (e) {
isMove = true;
// clientYy
// data.translateY
// moveClientY
moveClientY = e.clientY - data.translateY;
moveTo(); //
moveEnd(); //
};
// thumbscrollTop
const moveTo = function () {
document.onmousemove = (e) => {
//
if (isMove) {
//
if (e.clientY - moveClientY > trackHeight - data.barHeight) {
// translateY
data.translateY = trackHeight - data.barHeight;
} else if (e.clientY - moveClientY < 0) {
// translateY
data.translateY = 0;
} else {
//
data.translateY = e.clientY - moveClientY;
}
//
scrollRef.value.scrollTop = data.translateY / data.heightPre;
}
};
};
//
const moveEnd = function () {
document.onmouseup = (e) => {
if (isMove) {
isMove = false;
}
};
};
let dataRef = toRefs(data);
</script>