【问题标题】:support different screen size in phonegap/jquery mobile在phonegap/jquery mobile中支持不同的屏幕尺寸
【发布时间】:2013-05-06 07:06:06
【问题描述】:

在 android 中,我们有 layout_weight 和 layout_height 来设置图像/布局的宽度和高度,所以当我们有不同的屏幕尺寸时,它会自动调整到我们指定的屏幕宽度和屏幕高度。

他们在 phonegap/jquery mobile 中有什么相似之处吗?

我想要一个页眉和页脚以及介于两者之间的图像。我尝试在css中添加标题。但图像在标题中重复,整个屏幕也是可滚动的,但我想要屏幕中的固定图像,我想让图像自动调整到设备的屏幕尺寸。我是 phonegap 和 jquery mobile 的新手。

如何使图像固定在页眉和页脚之间并自动调整到不同的屏幕尺寸?

这里是html页面

<div data-role="page" id="timer">
     <div data-role="header"  data-position="fixed" data-tap-toggle="false" class="timerheader">
        <h1></h1>
     </div>
     <div data-role="content">
        <p><centre><img src="Image/timer.png" alt="" /></centre></p> 
     </div>
     <div data-role="footer" data-position="fixed" data-tap-toggle="false"   >
        <h3><a href="#" data-role="button" data-inline="true" ><img src="Image/Next.png"/></a></h3>
     </div>
</div>

谢谢:)

【问题讨论】:

  • 下次不要忘记添加jQuery Mobile标签,因为这是jQM问题,Phonegap在这里只是一个应用程序包装器。

标签: javascript jquery css cordova jquery-mobile


【解决方案1】:

这在 jQuery Mobile 中有点棘手,而页面宽度始终相同,页面高度会因页面事件而异。我不知道您对 jQuery Mobile 页面事件了解多少,简而言之,这些事件将在页面加载和页面转换期间发生(如果您想了解更多信息,请查看我的另一篇文章:jQuery Mobile: document ready vs page events)。

现在让我们回到您的问题。 jQM 页面内容永远不会占用整页内容,除非已经有足够的内容填充它并且我们不能采用 100% 宽度和 100% 高度的纯 css 方法。

为了解决这个问题,我们需要强制content div占据整个可用的页面内容高度,这可以通过这个函数来完成:

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;
}

我们将在 pageshow 页面事件期间使用此函数设置内容高度。并且图像将具有 100% 的宽度和 100% 的高度。

现在我们还需要修复图像 css 以使其正确适应整个内容的高度:

html,body{height:100%;}

img {
    padding: 0 !important;
    margin: 0 !important;
    border: 0 !important;
    overflow-y: hidden !important;
}

.ui-content {
    padding: 0 !important;
}

.ui-content img {  
    max-height: 100%;/* Part 1: Set a maxium relative to the parent */
    width: auto\9;
    /* IE7-8 need help adjusting responsive images */
    max-width: auto; 
    /* Part 2: Scale the height according to the width, otherwise you get stretching */
    vertical-align: middle;
    border: 0;
    -ms-interpolation-mode: bicubic;
}

最后这是一个工作示例:http://jsfiddle.net/Gajotres/yndsW/

编辑:

看看我的另一种方法。我决定不使用 img 标签,而是使用背景图像并让 css 处理居中。只需要Javascript来动态改变图像。

我们仍然需要 javascript 来修复我们的页面高度。

HTML:

<div data-role="page" id="Windage">
    <div data-theme="a" data-role="header" data-position="fixed">
        <h3>Header</h3>
    </div>


    <div data-role="content">

    </div>

    <div data-theme="a" data-role="footer" data-position="fixed">
        <h3>
            <a href="#" data-role="button" data-inline="true" data-transition="slide">NEXT</a>
        </h3>
    </div>
</div>

CSS:

html,body{
    height:100%;
}

.ui-content {
    background-repeat:no-repeat;
    background-size:contain;  
    background-position: center center;
}

Javascript:

$(document).on('pageshow', '#Windage', function(){       
    $('[data-role="content"]').height(getRealContentHeight());
    $('.ui-content').css('background-image','url(Image/timer.png)');    
});

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;
}

【讨论】:

  • 非常感谢您的回答。有用。但页眉和页脚之间的图像正在滚动。我必须在中心修复它。我怎样才能做到?。
  • 它在什么浏览器上滚动?你是在说我的例子吗?
  • 你的例子很完美。但我对我的项目实施了同样的事情并在设备上运行。但是我必须在页眉和页脚之间对齐的图像正在滚动。我该如何解决?
  • 先告诉我你有没有使用pageshow事件来修复内容高度?
  • Gajotres 我发现了问题所在。如果我使用这些线将图像与中心对齐,图像将滚动&lt;div data-role="content"&gt; &lt;p&gt;&lt;centre&gt;&lt;img src="Image/timer.png" alt="" /&gt;&lt;/centre&gt;&lt;/p&gt; &lt;/div&gt;,但如果我删除

    标签,它不会滚动。抱歉,当您第一次问我尝试不使用

    标签时,我发布了同样的内容。我为此道歉。但是如果我删除

    标签,图像就会向左倾斜。我将如何解决它。

【解决方案2】:

我知道其中一种解决方案。它并不完美,但效果非常好。您可以为每个分辨率指定不同的 css 文件并使用媒体查询。媒体查询将根据您可以指定的一些规则替换您的 css 代码,例如取决于屏幕分辨率。它适用于 PhoneGap 和 jQM。检查here 并阅读official w3 documentation.

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多