From 2c69d7a87f702b458aac0136b476246e248957e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferit=20Yi=C4=9Fit=20BALABAN?= Date: Wed, 27 Jul 2022 23:46:08 +0300 Subject: [PATCH] Refactor scroll-to-top mechanism --- resources/css/main.scss | 1 - resources/scripts/main.js | 14 +++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/resources/css/main.scss b/resources/css/main.scss index 7b06610..f0cc49a 100644 --- a/resources/css/main.scss +++ b/resources/css/main.scss @@ -270,7 +270,6 @@ a { #scroll-to-top { width: $px_scroll-to-top_size; height: $px_scroll-to-top_size; - display: none; position: fixed; right: 25px; bottom: 55px; diff --git a/resources/scripts/main.js b/resources/scripts/main.js index c746052..c111e12 100644 --- a/resources/scripts/main.js +++ b/resources/scripts/main.js @@ -1,10 +1,11 @@ +const SCROLL_MIN = 100; scroll_to_top = document.getElementById("scroll-to-top"); -window.onscroll = function () { - if (document.documentElement.scrollTop > 100 || document.body.scrollTop > 100) { - scroll_to_top.style.display = "block"; +function checkScroll() { + if (document.documentElement.scrollTop > SCROLL_MIN || document.body.scrollTop > SCROLL_MIN) { + scroll_to_top.style.visibility = "visible"; } else { - scroll_to_top.style.display = "none"; + scroll_to_top.style.visibility = "hidden"; } } @@ -60,4 +61,7 @@ async function toggleDropdown() { hideDropdown(); hideNavitems(); } -} \ No newline at end of file +} + +window.addEventListener('load', checkScroll); +window.addEventListener('scroll', checkScroll); \ No newline at end of file