在html5开发中,发现这么一个问题:每次返回上一个页面时,总是返回到上一个页面的顶部。大部分时间是无所谓的,但是如果遇到列表比较长时,体验就很糟糕了。所以得想办法解决这个问题。
花了很长时,查了很多资料都没有发现问题出在哪。最后通过断点调试,发现问题出在jquery mobile在页面加载完成后,会自动调用silentScroll函数
window.scrollTo( 0, ypos );
,所以会出现上述问题。
<span style="white-space:pre"> </span>$.event.special.scrollstart.enabled = false;
setTimeout(function() {
window.scrollTo( 0, ypos );
$.mobile.document.trigger( "silentscroll", { x: 0, y: ypos });
}, 20 );
setTimeout(function() {
$.event.special.scrollstart.enabled = true;
}, 150 );
通过查找,发现调用语句是:
// window load event
// hide iOS browser chrome on load if hideUrlBar is true this is as fall back incase we were too early before
if ( $.mobile.hideUrlBar ) {
$window.load( $.mobile.silentScroll );
}
目前的解决方案是注释掉$window.load( $.mobile.silentScroll );,就能达到想要效果了