62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
export const resetPageScroll = () => {
|
|
const scrollTop = () => {
|
|
uni.pageScrollTo({ scrollTop: 0, duration: 0 });
|
|
if (typeof window !== 'undefined') {
|
|
window.scrollTo({ top: 0, left: 0, behavior: 'auto' });
|
|
document.documentElement.scrollTop = 0;
|
|
document.body.scrollTop = 0;
|
|
}
|
|
};
|
|
|
|
scrollTop();
|
|
setTimeout(scrollTop, 0);
|
|
setTimeout(scrollTop, 80);
|
|
setTimeout(scrollTop, 240);
|
|
};
|
|
|
|
export const scrollPageToSelector = (selector: string, offset = 16) => {
|
|
const scrollToTarget = () => {
|
|
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
const element = document.querySelector<HTMLElement>(selector);
|
|
if (element) {
|
|
const scrollTop = Math.max(0, window.scrollY + element.getBoundingClientRect().top - offset);
|
|
uni.pageScrollTo({ scrollTop, duration: 0 });
|
|
window.scrollTo({ top: scrollTop, left: 0, behavior: 'auto' });
|
|
document.documentElement.scrollTop = scrollTop;
|
|
document.body.scrollTop = scrollTop;
|
|
return;
|
|
}
|
|
}
|
|
|
|
uni.createSelectorQuery()
|
|
.select(selector)
|
|
.boundingClientRect((rect) => {
|
|
const target = Array.isArray(rect) ? rect[0] : rect;
|
|
if (target && typeof target.top === 'number') {
|
|
uni.pageScrollTo({ scrollTop: Math.max(0, target.top - offset), duration: 0 });
|
|
}
|
|
})
|
|
.exec();
|
|
};
|
|
|
|
scrollToTarget();
|
|
setTimeout(scrollToTarget, 0);
|
|
setTimeout(scrollToTarget, 80);
|
|
setTimeout(scrollToTarget, 240);
|
|
};
|
|
|
|
const switchToToday = () => {
|
|
uni.switchTab({ url: '/pages/user/today/index' });
|
|
};
|
|
|
|
export const navigateBackToToday = () => {
|
|
if (getCurrentPages().length <= 1) {
|
|
switchToToday();
|
|
return;
|
|
}
|
|
uni.navigateBack({
|
|
delta: 1,
|
|
fail: switchToToday
|
|
});
|
|
};
|