gpt4 book ai didi

javascript - 在 phonegap/jquery mobile 中支持不同的屏幕尺寸

转载 作者:太空狗 更新时间:2023-10-29 12:36:12 25 4
gpt4 key购买 nike

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

在 phonegap/jquery mobile 中它们有什么类似的东西吗?

我想要一个页眉和页脚以及中间的图像。我尝试在 css 中添加 header 。但图像在标题中重复出现,而且整个屏幕都是可滚动的,但我希望屏幕上有一个固定图像,并且我想让图像自动调整为设备的屏幕尺寸。我是 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 中有点棘手,而页面宽度始终相同页面高度会因页面事件而异。我不知道您对 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;
}

关于javascript - 在 phonegap/jquery mobile 中支持不同的屏幕尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16393688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com