2

我正在开发一个 jQuery 移动项目,我想在其中使用全屏标题data-position="fixed" data-fullscreen="true"。我遇到的问题是标题正在切入内容。

我试图找出一种方法使内容随着标题的移动而移动。因此,当标题可见时,内容会被向下推,因此不会被截断,而当标题不可见时,内容会被向上推以最小化空白。

据我所知,唯一的方法是margin-top使用 data-role 动态更改 div 的 css 规则content。我认为这很容易,但事实并非如此。

这是我的html:

<div data-role="page" id="index">
    <div data-role="header" data-position="fixed" data-fullscreen="true" id='header'>Some Header Stuff</div>
    <div data-role="content" id="content">Some Content Stuff</div>
    <div data-role="footer" data-position="fixed" data-fullscreen="true">Some Footer Stuff</div>
</div>

到目前为止,我已经尝试了以下 jQuery 解决方案,但没有成功:

jQuery(document).on('pagecreate', function () {
    $("#content").css('margin-top', $('#header').height());
});

$(document).on('pageshow', '#index',function(e,data){   
    $('#content').height(getRealContentHeight()); 
});

function getRealContentHeight() {
    var header = $.mobile.activePage.find("div[data-role='header']:visible");
    var footer = $.mobile.activePage.find("div[data-role='footer']:visible");
    var content = $.mobile.activePage.find("div[data-role='content']:visible:visible");
    var viewport_height = $(window).height();

    var content_height = viewport_height - header.outerHeight() - footer.outerHeight();
    if((content.outerHeight() - header.outerHeight() - footer.outerHeight()) <= viewport_height) {
        content_height -= (content.outerHeight() - content.height());
    } 
    return content_height;
}

这两种解决方案都试图捕获标题的高度属性,并且据我所知,设置为的标题data-position="fixed" data-fullscreen="true"始终显示高度为 0。

我见过其他解决方案,例如添加一堆<br/>标签或添加一个空 div。当标题不可见时,这些解决方案都保留空白区域。有谁知道如何根据标题是否可见实际使标题上下切换内容?

4

1 回答 1

0

想出了一个很好的解决方案

$(document).on('pageshow', function () {
    $("#content").css('margin-top', $('#header').height());
});

$(document).on('vmouseup', '#index',function () {
    setTimeout(function() {
        $("#content").css('margin-top', $('#header').height());
    }, 300);    
});

上面的代码在 pagecreate 时设置内容的边距,并在用户单击通常会折叠标题的任何位置时重新设置边距。

于 2015-05-15T20:14:04.917 回答