🐛(component): 修复 scroll 组件无法监听 subtree 的节点变化

This commit is contained in:
就眠儀式 2022-09-01 00:55:29 +08:00
parent eb5dd1e812
commit fa37943b18
3 changed files with 30 additions and 40 deletions

View File

@ -6,7 +6,6 @@
.layui-scroll-y { .layui-scroll-y {
position: relative; position: relative;
height: 100%; height: 100%;
.layui-scroll-wrap { .layui-scroll-wrap {
height: 100%; height: 100%;
overflow-y: scroll; overflow-y: scroll;

View File

@ -12,7 +12,7 @@
ref="barRef" ref="barRef"
class="layui-scroll-track" class="layui-scroll-track"
:style="{ :style="{
backgroundColor: data.heightPre == 1 ? 'rgba(0,0,0,0)' : trackColor, backgroundColor: data.heightPre == 1 ? 'transparent' : trackColor,
}" }"
> >
<div <div
@ -20,7 +20,7 @@
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 ? 'transparent' : thumbColor,
}" }"
class="layui-scroll-thumb" class="layui-scroll-thumb"
@mousedown.stop.prevent="moveStart" @mousedown.stop.prevent="moveStart"
@ -101,22 +101,16 @@ const monitorWindow = function () {
// //
const monitorScrollBar = function () { const monitorScrollBar = function () {
// @ts-ignore // @ts-ignore
var monitorUl = scrollRef.value; let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
let MutationObserver = const observer : any = new MutationObserver(mutations => {
window.MutationObserver || initScrollListner();
// @ts-ignore })
window.WebKitMutationObserver ||
// @ts-ignore observer.observe(scrollRef.value, {
window.MozMutationObserver;
let observer = new MutationObserver((mutations) => {
initScrollListner();
});
if (monitorUl) {
observer.observe(monitorUl, {
attributes: true, attributes: true,
childList: true, childList: true,
}); subtree: true
} })
}; };
// //
@ -143,9 +137,7 @@ const initScrollListner = function () {
wrapContentHeight = scroll.scrollHeight; wrapContentHeight = scroll.scrollHeight;
wrapHeight = scroll.clientHeight; wrapHeight = scroll.clientHeight;
trackHeight = bar.clientHeight; trackHeight = bar.clientHeight;
// / 100 150
data.heightPre = wrapHeight / wrapContentHeight; data.heightPre = wrapHeight / wrapContentHeight;
//
data.barHeight = data.heightPre * trackHeight; data.barHeight = data.heightPre * trackHeight;
} }
}; };
@ -213,6 +205,4 @@ const moveEnd = function () {
} }
}; };
}; };
let dataRef = toRefs(data);
</script> </script>

View File

@ -15,32 +15,33 @@
<template> <template>
<lay-button @click="changeTotal">修改数据</lay-button> <lay-button @click="changeTotal">修改数据</lay-button>
<lay-button @click="changeMaxTotal">修改数据</lay-button> <lay-button @click="changeMaxTotal">修改数据</lay-button>
<lay-scroll height="200px" style="background-color:whitesmoke;height: calc(100% - 62px);"> <lay-scroll height="200px" style="background-color: whitesmoke" thumbColor="#000000">
<lay-panel v-for="(n,index) in total" :key="n" style="margin:10px;padding:10px;">内容</lay-panel> <lay-container>
<lay-row>
<lay-col span="24">
<lay-panel
v-for="(n, index) in total"
:key="n"
style="margin: 10px; padding: 10px"
>内容</lay-panel
>
</lay-col>
</lay-row>
</lay-container>
</lay-scroll> </lay-scroll>
</template> </template>
<script> <script lang="ts" setup>
import { ref } from 'vue' import { ref } from 'vue'
export default { const total = ref(50)
setup() {
const total = ref(50);
const changeTotal = () => { const changeTotal = () => {
total.value = 2; total.value = 2
} }
const changeMaxTotal = () => { const changeMaxTotal = () => {
total.value = 50; total.value = 50
}
return {
total,
changeTotal,
changeMaxTotal
}
}
} }
</script> </script>