我试图将页面当前值的偏移量保存在全局变量中,并尝试在页面重新加载时使用它滚动回同一位置。
这是我的代码
<script>
$(document).on("pagebeforeshow", '#outerPage', function(){
$(document).on('vclick','#outerPage',function(e){
//alert('yo i am clickable now');
var parentOffset = $(this).parent().offset();
//or $(this).offset(); if you really just want the current element's offset
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
storePosition.topCoordinate = relY;
});
var positionY = storePosition.topCoordinate ;
if(positionY !== null) {
setTimeout(function () {
$.mobile.defaultHomeScroll = positionY;
}, 10);
}
});
var storePosition = {
topCoordinate : null
}
</script>
在这里我试图将偏移量存储在storePosition.topCoordinate
然后在函数中使用它来设置页面滚动。但我的变量始终设置为 null 我不明白为什么会发生这种情况。
UPDATE
现在我正在使用类似这样的脚本
$(document).on('pageinit', '#outerPage', function () {
$('a').on('vclick', function () {
console.log('its done Boy');
localStorage.setItem('scrolls', 0);
});
});
/*$(window).on('unload', function() {
console.log(window.location.pathname);
console.log(window.location.pathname);
console.log( "Bye now!" );
})*/
$(document).on('pageinit', '#outerPage', function () {
$(document).on("scrollstop", function () {
var parentOffset = $(this).parent().offset();
var relY = window.scrollY;
localStorage.setItem('scrolls', relY);
console.log(localStorage.getItem("scrolls"));
});
});
$(document).on("pagebeforeshow", '#outerPage', function () {
var positionY = localStorage.getItem("scrolls");
if (positionY != null) {
$.mobile.defaultHomeScroll = positionY;
}
});
因此,我将值保存在 localStorage 中,并在更改页面时清除它,例如单击链接标签时,我知道这不是正确的解决方案:P。我试图使用 .unload() 但刷新同一页面时没有清除 LocalStorage 事件
这个你能帮我吗
感谢和问候