UNPKG

vanillajs-browser-helpers

Version:

Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser

35 lines (34 loc) 1.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const viewport_1 = __importDefault(require("./viewport")); const isWindow_1 = __importDefault(require("./isWindow")); /** * Gather the current scroll position information of a DOM element or the window * * @param elm - The element to find the scrolling position from * @return The scroll information */ function scrollInfo(elm = window) { const _elm = (0, isWindow_1.default)(elm) ? (0, viewport_1.default)(elm) : elm; const info = { x: 0, y: 0, xMax: 0, yMax: 0, xPct: 0, yPct: 0 }; if (!_elm) { return info; } info.x = _elm.scrollLeft; info.y = _elm.scrollTop; info.xMax = Math.max(_elm.scrollWidth - _elm.clientWidth, 0); info.yMax = Math.max(_elm.scrollHeight - _elm.clientHeight, 0); if (info.xMax) { info.xPct = info.x / info.xMax; } if (info.yMax) { info.yPct = info.y / info.yMax; } return info; } exports.default = scrollInfo;