これは、jQuery Mobile では少し注意が必要です。ページの幅は常に同じですが、ページの高さはページ イベントごとに異なります。jQuery Mobile ページ イベントについてどれだけ知っているかわかりませんが、簡単に言うと、これらのイベントは、ページの読み込み中およびページの遷移中に発生します (詳しく知りたい場合は、私の別の記事: jQueryをご覧ください)。モバイル: ドキュメント準備完了とページ イベント)。
それでは、質問に戻りましょう。jQM ページのコンテンツは、すでに十分なコンテンツが満たされていない限り、完全なページ コンテンツを取得することはありません。幅 100%、高さ 100% の純粋な CSS アプローチを採用することはできません。
この問題を解決するには、コンテンツ 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;
}