UNPKG

xxm-test-js

Version:
50 lines 2.18 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.ScrollDirectionChecker = void 0; /** * 用于检测页面滚动方向的工具类。 * 该类提供了一个静态方法来获取当前的滚动方向,不包含事件监听,需要时手动调用。 * * @returns {'up' | 'down' | 'top' | 'bottom'} 当前的滚动方向,可能的值为 'up'(向上滚动)、'down'(向下滚动)、'top'(页面顶部)、'bottom'(页面底部)。 * @example * ```javascript * const direction = ScrollDirectionChecker.getDirection(); * console.log('当前滚动方向:', direction); * ``` */ class ScrollDirectionChecker { // 使用更通用的方法获取滚动位置 static getScrollPosition() { var _b, _c, _d; return (_d = (_c = (_b = window.pageYOffset) !== null && _b !== void 0 ? _b : document.documentElement.scrollTop) !== null && _c !== void 0 ? _c : document.body.scrollTop) !== null && _d !== void 0 ? _d : 0; } static getDirection() { const currentPosition = this.getScrollPosition(); // 1. 检测是否在顶部/底部 if (currentPosition <= 0) { this.lastDirection = 'top'; } else if (currentPosition + window.innerHeight >= document.documentElement.scrollHeight - 1) { this.lastDirection = 'bottom'; } // 2. 计算方向变化 else { const diff = currentPosition - this.lastScrollPosition; if (Math.abs(diff) > this.config.ignoreThreshold) { // 使用配置项忽略微小滚动 this.lastDirection = diff > 0 ? 'down' : 'up'; } } this.lastScrollPosition = currentPosition; return this.lastDirection; } } exports.ScrollDirectionChecker = ScrollDirectionChecker; _a = ScrollDirectionChecker; // 添加配置项 ScrollDirectionChecker.config = { ignoreThreshold: 1 }; // 修改初始化代码 ScrollDirectionChecker.lastScrollPosition = _a.getScrollPosition(); ScrollDirectionChecker.lastDirection = _a.lastScrollPosition <= 0 ? 'top' : 'down'; //# sourceMappingURL=ScrollDirectionChecker.js.map