UNPKG

@antmjs/vantui

Version:

一套适用于Taro3及React的vantui组件库

28 lines 1.35 kB
import { useEffect } from 'react'; import { usePageScroll as useTaroPageScroll } from '@tarojs/taro'; import { Current } from '@tarojs/runtime'; export function usePageScroll(scroller) { useEffect(function () { var _Current$page, _Current$page$path; // 兼容react-ui 弄出来的代码 if (process.env.TARO_ENV !== 'h5') return; var id = Current === null || Current === void 0 ? void 0 : (_Current$page = Current.page) === null || _Current$page === void 0 ? void 0 : (_Current$page$path = _Current$page.path) === null || _Current$page$path === void 0 ? void 0 : _Current$page$path.replace(/([^a-z0-9\u00a0-\uffff_-])/gi, '\\$1'); var el = id ? document.querySelector(".taro_page#".concat(id)) : document.querySelector('.taro_page') || document.querySelector('.taro_router') || document.scrollingElement; function listener(event) { if (!event.target) return; var _event = { scrollTop: el === null || el === void 0 ? void 0 : el.scrollTop, scrollLeft: el === null || el === void 0 ? void 0 : el.scrollLeft }; scroller(_event); } el.addEventListener('scroll', listener); return function () { el.removeEventListener('scroll', listener); }; }, [scroller]); useTaroPageScroll(function (e) { if (process.env.TARO_ENV === 'h5') return; scroller(e); }); }