Skip to content

Commit

Permalink
scroll to top added
Browse files Browse the repository at this point in the history
  • Loading branch information
GitPaulo committed Oct 26, 2024
1 parent 8b91743 commit 717caa0
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,21 @@
showTitle = !showTitle;
if (!showTitle) {
const actionBar = document.getElementById("action-bar");
if (actionBar) {
actionBar.style.position = "fixed";
}
detachActionBar();
}
}
function detachActionBar() {
const actionBar = document.getElementById("action-bar");
if (actionBar) {
actionBar.style.position = "fixed";
}
}
function attachActionBar() {
const actionBar = document.getElementById("action-bar");
if (actionBar) {
actionBar.style.position = "absolute";
}
}
Expand All @@ -160,7 +171,6 @@
}
// TODO: Move this into a tooltip component
function enableToggleTooltip() {
showToggleTooltip = true;
}
Expand All @@ -185,6 +195,13 @@
showCurrentTooltip = false;
}
function scrollToTop() {
const contentContainer =
document.getElementsByClassName("content-container")[0];
if (!contentContainer) return;
contentContainer.scrollTo({ top: 0, behavior: "smooth" });
}
function scrollToLastCheckedQuest() {
if (lastCheckedQuestId === null) return;
if (searchQuery) {
Expand Down Expand Up @@ -226,6 +243,21 @@
});
}
let showScrollToTop = false;
function handleScroll() {
const contentContainer =
document.getElementsByClassName("content-container")[0];
if (!contentContainer) return;
if (!showTitle) return;
showScrollToTop = contentContainer.scrollTop > 140;
if (showScrollToTop) {
detachActionBar();
} else {
attachActionBar();
}
}
function createMagicParticles(inputElement: HTMLInputElement) {
const container = inputElement.closest("li");
if (!container) return;
Expand Down Expand Up @@ -296,10 +328,16 @@
// Events
window.addEventListener("keydown", handleKeydown);
document
.getElementsByClassName("content-container")[0]
.addEventListener("scroll", handleScroll);
});
onDestroy(() => {
window.removeEventListener("keydown", handleKeydown);
document
.getElementsByClassName("content-container")[0]
.removeEventListener("scroll", handleScroll);
});
// Reactively update the background whenever the current expansion changes
Expand Down Expand Up @@ -413,6 +451,16 @@
<span class="block sm:hidden">Goto Quest</span>
<span class="hidden sm:block">Scroll to Current Quest</span>
</button>

<!-- Scroll Top -->
{#if showScrollToTop}
<button
on:click={scrollToTop}
class="ml-4 p-2 rounded-lg shadow transition-colors duration-300 text-xs sm:text-base bg-white text-gray-700 border border-gray-300 hover:bg-gray-100"
>
</button>
{/if}
</div>

<!--- Progress --->
Expand Down

0 comments on commit 717caa0

Please sign in to comment.