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

View File

@ -10,4 +10,4 @@ export const withInstall = <T>(comp: T): T & Plugin => {
};
return component as T & Plugin;
};
};