Files
past-go/weapp/utils/scroll-pos.js
T
2026-10-07 09:28:43 +08:00

43 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 页面滚动位置管理工具
* 保存和恢复底部导航栏页面的滚动位置,切换 Tab 后返回时保持浏览位置
*/
const SCROLL_POS_KEY = 'tabScrollPositions';
/**
* 保存指定页面的滚动位置
* @param {string} pageKey - 页面标识(index/diary/profile)
* @param {number} scrollTop - scroll-view 的滚动位置
*/
function saveScrollPosition(pageKey, scrollTop) {
try {
var positions = wx.getStorageSync(SCROLL_POS_KEY) || {};
positions[pageKey] = {
scrollTop: scrollTop,
timestamp: Date.now()
};
wx.setStorageSync(SCROLL_POS_KEY, positions);
} catch (e) {}
}
/**
* 获取指定页面保存的滚动位置
* @param {string} pageKey - 页面标识
* @returns {number} 保存的 scrollTop,没有则返回 0
*/
function getScrollPosition(pageKey) {
try {
var positions = wx.getStorageSync(SCROLL_POS_KEY) || {};
var pos = positions[pageKey];
return pos ? (pos.scrollTop || 0) : 0;
} catch (e) {
return 0;
}
}
module.exports = {
saveScrollPosition,
getScrollPosition
};