所以我正在开发一个 jQuery 手机应用程序,我注意到每次打开面板时主页都会自动滚动到顶部。我希望当您打开面板时页面保留在原处。
经过一番谷歌搜索后,我唯一发现的是: github 这并不令人鼓舞,这是: page-scrolls-back-to-top-when-the-menu-panel-is-opened-in-jquery-mobile 这对我不起作用。
以前有人解决过这个问题吗?我是 jQuery 新手,所以我不认为我可以从头开始编写一些东西,但朝着正确的方向轻推会很棒。
这是我正在使用的代码:
var panel = '<div data-role="panel" id="left-panel" class="jqm-navmenu-panel" data-position-fixed="true" data-position="left" data-display="push" data-dismissible="true" data-theme="b"><ul data-role="listview"><div id="profile_pic"></div><div id="auth-loggedin" style="display: none"><p class="name_caption"><li><a href="#">Log Out</a></li></ul></div><!-- /leftpanel2 -->';
$(document).one('pagebeforecreate', function () {
$.mobile.pageContainer.prepend(panel).enhanceWithin();
$("#left-panel").panel();
});
这是 html
<div data-role="header" data-position="fixed">
<div id="i">
<div id="header_img_1"><p>Logo 1</p></div>
<div id="header_img_2"><p>Logo 2</p></div>
</div>
<a href="#left-panel" class="jqm-navmenu-link ui-btn ui-btn-icon-notext ui-corner-all ui-icon-bars ui-btn-left" data-icon="bars" data-iconpos="notext">Menu</a>
</div><!-- /header -->
<div data-role="content">
<div id="auth-status">
<div id="auth-loggedout">
<div class="fb-login-button" autologoutlink="true" scope="email,user_checkins">Login with Facebook</div>
</div>
</div>
<div id="auth-loggedin" style="display: none">
Welcome , <span id="auth-displayname"></span>(<a href="#" id="auth-logoutlink">logout</a>)
</div>
</div><!-- /content -->
<div data-role="footer" data-position="fixed" data-theme="b">
<p class="jqm-version"></p>
<p class="footer_text">Content TBD</p>
</div>
</div>
最佳答案
这就是 JQM 目前的工作方式,直到版本 2 为止,直到滚动物理到位。
目前有一些方法可以解决这个问题。如果面板上有很多项目,那么我建议使用 Iscroll 5 http://cubiq.org/iscroll-5或者,如果您已经使用 Iscroll 插入,则为面板创建一个滚动条。
但是,如果项目不是那么多并且您没有使用 Iscroll 插入,则以下方法效果很好。
方法A.使用CSS使面板内容的内部部分独立于主内容页滚动,避免双重滚动。
CSS 修复 JQM 1.3、1.4+
.ui-panel.ui-panel-open {
position:fixed;
}
.ui-panel-inner {
position: absolute;
top: 1px;
left: 0;
right: 0;
bottom: 0px;
overflow: scroll;
-webkit-overflow-scrolling: touch;
}
我的面板中有这个
data-position-fixed="true"
仅适用于 1.3,这是一个小技巧,用于在面板向右滚动到顶部或向右底部并且用户继续滚动时停止主页滚动。
.ui-page-active.ui-page-panel {
height: 70%;
}
演示
方法 B. 因为当面板打开时 ListView 会在顶部滚动,所以此方法会记住主 ListView 上的最后一个滚动位置,并在面板关闭时为其设置动画。
CSS
::-webkit-scrollbar { width: 0px; }
.ui-panel.ui-panel-open {
position:fixed;
}
.ui-panel-inner {
position: absolute;
width:100%;
top: 0px;
bottom: -17px;
overflow: scroll;
-webkit-overflow-scrolling: touch;
}
J-query。存储(滚动时的 ListView )的位置。
请注意 storePosition.topCoordinate !== 0 条件的存在,因此如果我们位于 ListView 的顶部,我们可以避免动画以节省一些 CPU 周期。
var storePosition = {
topCoordinate : null
}
$(document).ready(function(){
$( "#mypanel" ).panel({
beforeopen: function( event, ui ) {
storePosition.topCoordinate = $(this).offset().top;
$( ".ui-mobile [data-role=page], .ui-mobile [data-role=dialog], .ui-page" ).css("position","fixed");
}
});
$( "#mypanel" ).panel({
close: function( event, ui ) {
$( ".ui-mobile [data-role=page], .ui-mobile [data-role=dialog], .ui-page" ).css("position","");
if($.mobile.activePage.attr("id") == "index" && storePosition.topCoordinate !== 0){
$('html, body').animate({
scrollTop: $("#mainlist").position().top += storePosition.topCoordinate - 60
}, 1000);
}
}
});
});
DEMO 1,面板关闭后滚动一秒动画。向下滚动任意位置,打开面板,然后关闭面板。
DEMO 2面板关闭前即时动画滚动。向下滚动任意位置,打开面板,然后关闭面板。
更改
$( "#mypanel" ).panel({
beforeclose: function( event, ui ) {
$( ".ui-mobile [data-role=page], .ui-mobile [data-role=dialog], .ui-page" ).css("position","");
if($.mobile.activePage.attr("id") == "index" && storePosition.topCoordinate !== 0){
$('html, body').animate({
scrollTop: $("#mainlist").position().top += storePosition.topCoordinate - 60
}, 10);
}
}
});
})
2014 年 11 月 16 日更新。
这里有一个很好的指南,介绍如何实现 native 滚动,即使您从一个页面导航到下一页,它也会保持滚动位置
http://outof.me/native-scrolling-in-jquery-mobilephonegap-applications/
该演示适用于旧版本的 JQM,但在最新的 1.4.5 上运行良好
为了使面板独立工作,您需要添加以下 css
.ui-panel-inner {
position: absolute;
width: 240px;
max-width: 240px;
top: 0px;
bottom: 0px;
overflow: scroll;
-webkit-overflow-scrolling: touch;
}
如果您想让面板大于默认宽度:17em;
,请在 .ui-panel-inner 中调整宽度和最大宽度
演示
关于jQuery移动面板打开卷轴页面到顶部,如何改变这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22672236/