Skip to content

Commit

Permalink
remove scroll-to-top on bottom of page
Browse files Browse the repository at this point in the history
  • Loading branch information
dermatthes committed Oct 23, 2023
1 parent 4390322 commit 46aac3a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/scroll-to-top/scroll-to-top.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {customElement} from "@kasimirjs/embed";
import {customElement, Debouncer} from "@kasimirjs/embed";
import {sleep} from "@micx/lib-js/src/helper/functions";


let debounceer = new Debouncer(100);

@customElement("liscom-scroll-to-top")
class ScrollToTop extends HTMLElement {



connectedCallback() {

console.log("scroll to top");
this.addEventListener("click", () => {
window.scrollTo({top: 0, behavior: "smooth"});
Expand All @@ -16,13 +20,19 @@ class ScrollToTop extends HTMLElement {
if (this.innerHTML.trim() === "")
this.innerHTML = "⬆️";

window.addEventListener("scroll", () => {
window.addEventListener("scroll", async () => {
await debounceer.debounce();
console.log("scroll", window.innerHeight, window.getComputedStyle(document.body).height, window.scrollY);
if (window.scrollY > 300 && active === false) {
this.style.display = "block";
await sleep(200);
this.classList.add("show");
active = true;
}
if (window.scrollY < 300 && active === true) {
if ((window.scrollY < 300 || window.scrollY > parseInt(window.getComputedStyle(document.body).height) - window.outerHeight - 500)&& active === true) {
this.classList.remove("show");
await sleep(500);
this.style.display = "none";
active = false;
}
}, {passive: true});
Expand Down

0 comments on commit 46aac3a

Please sign in to comment.