(component): 优化 scroll 组件内容改变时重新计算滚动条

This commit is contained in:
就眠儀式 2022-08-16 09:37:56 +08:00
parent 48966c93ec
commit 3f608a5935
2 changed files with 44 additions and 38 deletions

View File

@ -1,30 +1,18 @@
<template> <template>
<div <div class="layui-scroll" :class="{ hide: data.winWidth < 500 }" :style="{ height: height }">
class="layui-scroll"
:class="{ hide: data.winWidth < 500 }"
:style="{ height: height }"
>
<div class="layui-scroll-y"> <div class="layui-scroll-y">
<div ref="scrollRef" class="layui-scroll-wrap" @scroll="onMosewheel"> <div ref="scrollRef" class="layui-scroll-wrap" @scroll="onMosewheel">
<slot></slot> <slot></slot>
</div> </div>
<div <div ref="barRef" class="layui-scroll-track" :style="{
ref="barRef"
class="layui-scroll-track"
:style="{
backgroundColor: data.heightPre == 1 ? 'rgba(0,0,0,0)' : trackColor, backgroundColor: data.heightPre == 1 ? 'rgba(0,0,0,0)' : trackColor,
}" }">
> <div :style="{
<div
:style="{
height: data.barHeight + 'px', height: data.barHeight + 'px',
width: thumbWidth + 'px', width: thumbWidth + 'px',
transform: 'translateY(' + data.translateY + 'px)', transform: 'translateY(' + data.translateY + 'px)',
backgroundColor: data.heightPre == 1 ? 'rgba(0,0,0,0)' : thumbColor, backgroundColor: data.heightPre == 1 ? 'rgba(0,0,0,0)' : thumbColor,
}" }" class="layui-scroll-thumb" @mousedown.stop.prevent="moveStart"></div>
class="layui-scroll-thumb"
@mousedown.stop.prevent="moveStart"
></div>
</div> </div>
</div> </div>
</div> </div>
@ -74,7 +62,7 @@ let wrapHeight = 0; // 容器高度(可视高度)
let wrapContentHeight = 0; // let wrapContentHeight = 0; //
onMounted(() => { onMounted(() => {
monitorWindow() monitorWindow();
monitorScrollBar(); monitorScrollBar();
nextTick(() => { nextTick(() => {
calculationLength(); calculationLength();
@ -88,13 +76,11 @@ onUnmounted(() => {
// //
const monitorWindow = function () { const monitorWindow = function () {
let time: NodeJS.Timeout; // let time: NodeJS.Timeout;
window.addEventListener("resize", () => { window.addEventListener("resize", () => {
data.winWidth = document.body.clientWidth; // data.winWidth = document.body.clientWidth;
clearTimeout(time); clearTimeout(time);
time = setTimeout(() => { time = setTimeout(() => {
//500
// console.log("");
initScrollListner(); initScrollListner();
}, 500); }, 500);
}); });
@ -103,16 +89,22 @@ const monitorWindow = function () {
// //
const monitorScrollBar = function () { const monitorScrollBar = function () {
// @ts-ignore // @ts-ignore
var monitorUl = scrollRef.value.children[0]; var monitorUl = scrollRef.value;
let MutationObserver =
window.MutationObserver ||
// @ts-ignore // @ts-ignore
let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; window.WebKitMutationObserver ||
// @ts-ignore
window.MozMutationObserver;
let observer = new MutationObserver((mutations) => { let observer = new MutationObserver((mutations) => {
initScrollListner(); initScrollListner();
}); });
if (monitorUl) {
observer.observe(monitorUl, { observer.observe(monitorUl, {
attributes: true, attributes: true,
childList: true, childList: true,
}); });
}
}; };
// //

View File

@ -13,8 +13,10 @@
::: demo 使用 `lay-scroll` 标签, 创建一个虚拟滚动容器 ::: demo 使用 `lay-scroll` 标签, 创建一个虚拟滚动容器
<template> <template>
<lay-button @click="changeTotal">修改数据</lay-button>
<lay-button @click="changeMaxTotal">修改数据</lay-button>
<lay-scroll height="200px" style="background-color:whitesmoke;"> <lay-scroll height="200px" style="background-color:whitesmoke;">
<lay-panel v-for="(n,index) in 50" :key="n" style="margin:10px;padding:10px;">内容</lay-panel> <lay-panel v-for="(n,index) in total" :key="n" style="margin:10px;padding:10px;">内容</lay-panel>
</lay-scroll> </lay-scroll>
</template> </template>
@ -23,8 +25,20 @@ import { ref } from 'vue'
export default { export default {
setup() { setup() {
const total = ref(50);
const changeTotal = () => {
total.value = 2;
}
const changeMaxTotal = () => {
total.value = 50;
}
return { return {
total,
changeTotal,
changeMaxTotal
} }
} }
} }