🐛(component): 修复 scroll 警告

This commit is contained in:
就眠儀式 2022-08-16 09:17:48 +08:00
parent 5abad236ee
commit 48966c93ec
3 changed files with 23 additions and 37 deletions

View File

@ -56,8 +56,8 @@ const props = withDefaults(defineProps<LayScrollProps>(), {
const emit = defineEmits(["arrive"]); const emit = defineEmits(["arrive"]);
const scrollRef = ref<HTMLElement | null>(null); const scrollRef = ref<HTMLElement | null | undefined>();
const barRef = ref<HTMLElement | null>(null); const barRef = ref<HTMLElement | null | undefined>();
const data = reactive({ const data = reactive({
translateY: 0, // translateY: 0, //
@ -66,32 +66,29 @@ const data = reactive({
winWidth: document.body.clientWidth, // winWidth: document.body.clientWidth, //
}); });
let time = null; // let time: NodeJS.Timeout; //
let isMove = false; // let isMove = false; //
let moveClientY = 0; // let moveClientY = 0; //
let trackHeight = 0; // let trackHeight = 0; //
let wrapHeight = 0; // let wrapHeight = 0; //
let wrapContentHeight = 0; // let wrapContentHeight = 0; //
//
onMounted(() => { onMounted(() => {
monitorWindow(); // monitorWindow()
monitorScrollBar(); // monitorScrollBar();
nextTick(() => { nextTick(() => {
//dom calculationLength();
calculationLength(); //
}); });
}); });
// //
onUnmounted(() => { onUnmounted(() => {
window.clearInterval(time); window.clearInterval(time);
time = null;
}); });
// //
const monitorWindow = function () { const monitorWindow = function () {
let time; // let time: NodeJS.Timeout; //
window.addEventListener("resize", () => { window.addEventListener("resize", () => {
data.winWidth = document.body.clientWidth; // data.winWidth = document.body.clientWidth; //
clearTimeout(time); clearTimeout(time);
@ -105,20 +102,13 @@ const monitorWindow = function () {
// //
const monitorScrollBar = function () { const monitorScrollBar = function () {
// @ts-ignore
var monitorUl = scrollRef.value.children[0]; var monitorUl = scrollRef.value.children[0];
// var monitorDiv= document; // document // @ts-ignore
let MutationObserver = let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
window.MutationObserver || let observer = new MutationObserver((mutations) => {
// @ts-ignore
window.WebKitMutationObserver ||
// @ts-ignore
window.MozMutationObserver;
let observer = new MutationObserver(function (mutations) {
// console.log("");
initScrollListner(); initScrollListner();
}); });
//
observer.observe(monitorUl, { observer.observe(monitorUl, {
attributes: true, attributes: true,
childList: true, childList: true,
@ -137,7 +127,6 @@ const calculationLength = function () {
// 500 // 500
setTimeout(() => { setTimeout(() => {
window.clearInterval(time); window.clearInterval(time);
time = null;
}, 2000); }, 2000);
}; };
@ -146,11 +135,10 @@ const initScrollListner = function () {
let scroll = scrollRef.value; let scroll = scrollRef.value;
let bar = barRef.value; let bar = barRef.value;
// scroll // scroll
if (scroll) { if (scroll && bar) {
wrapContentHeight = scroll.scrollHeight; wrapContentHeight = scroll.scrollHeight;
wrapHeight = scroll.clientHeight; wrapHeight = scroll.clientHeight;
trackHeight = bar.clientHeight; trackHeight = bar.clientHeight;
// console.log(wrapContentHeight ,wrapHeight);
// / 100 150 // / 100 150
data.heightPre = wrapHeight / wrapContentHeight; data.heightPre = wrapHeight / wrapContentHeight;
// //
@ -159,11 +147,7 @@ const initScrollListner = function () {
}; };
// //
const onMosewheel = function (e) { const onMosewheel = (e: any) => {
// scrollTop
// offsetHeight
// scrollHeight
// data.translateY
data.translateY = e.target.scrollTop * data.heightPre; data.translateY = e.target.scrollTop * data.heightPre;
if (data.translateY == 0) { if (data.translateY == 0) {
// //
@ -178,12 +162,12 @@ const onMosewheel = function (e) {
}; };
// //
const arrive = function name(tb) { const arrive = (tb: string) => {
emit("arrive", tb); emit("arrive", tb);
}; };
// //
const moveStart = function (e) { const moveStart = (e: any) => {
isMove = true; isMove = true;
// clientYy // clientYy
// data.translateY // data.translateY
@ -194,7 +178,7 @@ const moveStart = function (e) {
}; };
// thumbscrollTop // thumbscrollTop
const moveTo = function () { const moveTo = () => {
document.onmousemove = (e) => { document.onmousemove = (e) => {
// //
if (isMove) { if (isMove) {
@ -203,14 +187,16 @@ const moveTo = function () {
// translateY // translateY
data.translateY = trackHeight - data.barHeight; data.translateY = trackHeight - data.barHeight;
} else if (e.clientY - moveClientY < 0) { } else if (e.clientY - moveClientY < 0) {
// translateY // translateY
data.translateY = 0; data.translateY = 0;
} else { } else {
// //
data.translateY = e.clientY - moveClientY; data.translateY = e.clientY - moveClientY;
} }
// //
scrollRef.value.scrollTop = data.translateY / data.heightPre; if(scrollRef.value) {
scrollRef.value.scrollTop = data.translateY / data.heightPre;
}
} }
}; };
}; };

View File

@ -452,9 +452,9 @@ watch(
() => { () => {
nextTick(() => { nextTick(() => {
getScrollWidth(); getScrollWidth();
}) });
} }
) );
onMounted(() => { onMounted(() => {
getScrollWidth(); getScrollWidth();