@tarojs/router
Version:
Taro-router
38 lines (35 loc) • 1.5 kB
JavaScript
import { Current } from '@tarojs/runtime';
const pageScrollBindings = new Map();
function bindPageScroll(page, scrollEl, distance = 50) {
var _a;
const pagePath = (page ? page === null || page === void 0 ? void 0 : page.path : (_a = Current.router) === null || _a === void 0 ? void 0 : _a.path);
const previous = pageScrollBindings.get(pagePath);
previous === null || previous === void 0 ? void 0 : previous.target.removeEventListener('scroll', previous.listener);
let isReachBottom = false;
const listener = function () {
var _a;
(_a = page.onPageScroll) === null || _a === void 0 ? void 0 : _a.call(page, {
scrollTop: scrollEl instanceof Window ? scrollEl.scrollY : scrollEl.scrollTop
});
if (isReachBottom && getOffset(scrollEl) > distance) {
isReachBottom = false;
}
if (page.onReachBottom &&
!isReachBottom &&
getOffset(scrollEl) < distance) {
isReachBottom = true;
page.onReachBottom();
}
};
pageScrollBindings.set(pagePath, { target: scrollEl, listener });
scrollEl.addEventListener('scroll', listener, false);
}
function getOffset(scrollEl) {
if (scrollEl instanceof Window) {
return scrollEl.document.documentElement.scrollHeight - scrollEl.scrollY - scrollEl.innerHeight;
}
else {
return scrollEl.scrollHeight - scrollEl.scrollTop - scrollEl.clientHeight;
}
}
export { bindPageScroll };