From b49c7ed4a1645ef5baedf492bc0c69f2cf14d7a7 Mon Sep 17 00:00:00 2001 From: Allen Chiang Date: Wed, 6 Mar 2024 11:45:00 +0800 Subject: [PATCH] refactor: remove unnecessary code --- resources/ts/scroll-to-top-btn.ts | 33 +++++++++++-------- .../components/post-form/ckedtior.blade.php | 4 +-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/resources/ts/scroll-to-top-btn.ts b/resources/ts/scroll-to-top-btn.ts index ec65302a..cfa698b5 100644 --- a/resources/ts/scroll-to-top-btn.ts +++ b/resources/ts/scroll-to-top-btn.ts @@ -12,32 +12,39 @@ window.setupScrollToTopButton = function ( ): void { scrollToTopButton.addEventListener('click', scrollToTop); - // 監聽滾動 - window.addEventListener('scroll', () => { - if (window.scrollY > 0) { - scrollToTopButton.classList.add('xl:flex'); - } else { - scrollToTopButton.classList.remove('xl:flex'); - } - }); - + let header = document.getElementById('header'); let footer = document.getElementById('footer'); + // 根據 header 是否出現在畫面上調整按鈕的樣式 + let headerObserver = new IntersectionObserver( + function (entries) { + if (entries[0].isIntersecting) { + // header 在畫面上 + scrollToTopButton.classList.remove('xl:flex'); + } else { + // header 不在畫面上 + scrollToTopButton.classList.add('xl:flex'); + } + }, + { threshold: [0] }, + ); + // 根據 footer 是否出現在畫面上調整按鈕的樣式 let footerObserver = new IntersectionObserver( function (entries) { if (entries[0].isIntersecting) { // footer 在畫面上 - scrollToTopButton.classList.remove('fixed'); - scrollToTopButton.classList.add('absolute'); + scrollToTopButton.classList.remove('fixed', 'bottom-7'); + scrollToTopButton.classList.add('absolute', 'bottom-1'); } else { // footer 不在畫面上 - scrollToTopButton.classList.add('fixed'); - scrollToTopButton.classList.remove('absolute'); + scrollToTopButton.classList.add('fixed', 'bottom-7'); + scrollToTopButton.classList.remove('absolute', 'bottom-1'); } }, { threshold: [0] }, ); + headerObserver.observe(header); footerObserver.observe(footer); }; diff --git a/resources/views/components/post-form/ckedtior.blade.php b/resources/views/components/post-form/ckedtior.blade.php index 1bd8259d..7b4539fb 100644 --- a/resources/views/components/post-form/ckedtior.blade.php +++ b/resources/views/components/post-form/ckedtior.blade.php @@ -8,8 +8,8 @@ editorDebounceTimer: null, debounce(callback, time) { // https://webdesign.tutsplus.com/tutorials/javascript-debounce-and-throttle--cms-36783 - window.clearTimeout(this.editorDebounceTimer); - this.editorDebounceTimer = window.setTimeout(callback, time); + clearTimeout(this.editorDebounceTimer); + this.editorDebounceTimer = setTimeout(callback, time); }, imageUploadUrl: @js(route('images.store')), body: @entangle($model).live,