From 5fd171bab02fc177612e1503f7b8d1bfc96de5a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A9=BA=E6=A2=A6?= <44894906+EmptyDreams@users.noreply.github.com> Date: Wed, 5 Oct 2022 21:14:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3`throttle`=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 只有在调用`throttle`返回的同一个对象时才能起到“节流”效果。 --- source/js/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/js/main.js b/source/js/main.js index 707aca6..7f1e7ca 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -277,8 +277,7 @@ document.addEventListener('DOMContentLoaded', function () { const isChatBtnHide = typeof chatBtnHide === 'function' const isChatBtnShow = typeof chatBtnShow === 'function' - window.scrollCollect = () => { - return btf.throttle(function (e) { + const scroolTask = btf.throttle(() => { const currentTop = window.scrollY || document.documentElement.scrollTop const isDown = scrollDirection(currentTop) if (currentTop > 56) { @@ -309,8 +308,9 @@ document.addEventListener('DOMContentLoaded', function () { if (document.body.scrollHeight <= innerHeight) { $rightside.style.cssText = 'opacity: 0.8; transform: translateX(-58px)' } - }, 200)() - } + }, 200) + + window.scrollCollect = scrollTask window.addEventListener('scroll', scrollCollect) }