UNPKG

scroll-tracking-walkeros

Version:

Custom scroll tracking used as an additional source for WalkerOS.

63 lines (60 loc) 2.21 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { initScrollTracking: () => initScrollTracking, scrollReport: () => scrollReport }); module.exports = __toCommonJS(index_exports); // src/functions.ts var scrollMaximum = 0; var prevScrollPercent = null; function scrollReport() { const article = document.querySelector("#articlebody") || document.body; const articleLength = article.offsetHeight; const scrollDepth = window.scrollY || document.documentElement.scrollTop || document.body.scrollTop; let scrollPercent = Math.round(scrollDepth / articleLength * 100) / 100; if (scrollPercent < 1) { scrollMaximum = scrollPercent > scrollMaximum ? scrollPercent : scrollMaximum; } else { scrollMaximum = 1; } return { scrollPercent, scrollMaximum }; } function initScrollTracking(callback) { function handleScroll() { const { scrollPercent, scrollMaximum: scrollMaximum2 } = scrollReport(); if (scrollPercent !== prevScrollPercent) { callback({ scrollPercent, scrollMaximum: scrollMaximum2, previousPercent: prevScrollPercent }); prevScrollPercent = scrollPercent; } } window.addEventListener("scroll", handleScroll); return () => window.removeEventListener("scroll", handleScroll); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { initScrollTracking, scrollReport });