init: initial commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 页面滚动位置管理工具
|
||||
* 保存和恢复底部导航栏页面的滚动位置,切换 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
|
||||
};
|
||||
Reference in New Issue
Block a user