【问题标题】:jQuery mobile header cutting off contentjQuery移动标题截断内容
【发布时间】:2015-05-15 18:56:26
【问题描述】:

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

我正在想办法让内容随着标题的移动而移动。因此,当标题可见时,内容会被向下推,因此不会被截断;当标题不可见时,内容会被向上推以最小化空白。

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

这是我的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。

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

【问题讨论】:

    标签: jquery jquery-mobile jquery-mobile-pageshow


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      相关资源
      最近更新 更多