4

In android we have layout_weight and layout_height to set the width and height of an image/layout, So when we have different screen size it automatically adjust to the screen width and screen height which we have specified.

Is their anything similar to it in phonegap/jquery mobile?.

I want to have a header and footer and a image in between. I tried adding header in css. but the image is repeating in header and also the entire screen is scrollable but i want a fixed image in the screen and i want to make images to adjust automatically to screen size of the device. I am new to phonegap and jquery mobile.

How can i make image fixed in between header and footer and adjust automatically to different screen size?.

Here is html page

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

Thanks:)

Gajotres
  • 57,309
  • 16
  • 102
  • 130
Beginner
  • 1,414
  • 2
  • 21
  • 41
  • 1
    Next time don't forget to add jQuery Mobile tag because this is a jQM problem, Phonegap is just a app wrapper here. – Gajotres May 06 '13 at 09:49

2 Answers2

5

This is a little bit tricky in jQuery Mobile, while page width is always the same page height will vary from page event to page event. I don't know how much do you know about jQuery Mobile page events, to make story short those events will occur during to page load and during the page transitions (if you want to find out more take a look at my other article: jQuery Mobile: document ready vs page events).

Now lets get back to your question. jQM page content will never take full page content unless there's already enough content filling it and we cant take pure css approach with 100% width and 100 % height.

To solve this problem we need to force content div to take full available page content height, and it can be done with this function:

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

We will use this function to set content height during the pageshow page event. And image will have width 100% and height 100%.

Now we also need to fix image css so it will properly fit full content height:

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

And finally here's a working example: http://jsfiddle.net/Gajotres/yndsW/

EDIT :

Take a look at my another approach. Instead of using img tag I have decided to use background image and let css to handle centering. Javascript is only needed to dynamically change image.

We still need javascript to fix our page height.

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;
}
Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • thanks a lot for the answer. it works. but the image inbetween header and footer is scrolling. i have to fix it in centre. how can i make it?. – Beginner May 06 '13 at 11:13
  • It is scrolling on what browser? And are you talking about my example? – Gajotres May 06 '13 at 12:04
  • your example is perfect. but i implemented same thing to my project and ran on device. But my image which has to be aligned in between header and footer is scrolling. how can i fix it? – Beginner May 06 '13 at 12:07
  • First tell me have you used pageshow event to fix content height? – Gajotres May 06 '13 at 12:09
  • Yea.. i have followed all the steps which u have said in the working example. – Beginner May 06 '13 at 12:11
  • Can you add your page HTML to your question? Just the page where you are showing your image. – Gajotres May 06 '13 at 12:13
  • i have added html page. timer.png is having dimensions of 640x1136. – Beginner May 06 '13 at 12:17
  • Width = 640 and height = 1136? – Gajotres May 06 '13 at 12:41
  • Unfortunately I think something in your code is causing this to fail. Even with your page and same dimensions picture picture is shown in perfectly placed between header and footer. – Gajotres May 06 '13 at 12:48
  • i am using the same code which u have given in working example. But in my html page i have 3 pages. i have removed the other 2 pages and checked it. but still image is scrolling. i dont have any other code in my project. I tried to run on android simulator with API level 17. – Beginner May 06 '13 at 13:00
  • Are you testing it on a Android, iOS or Phonegap Ripple emulator? – Gajotres May 06 '13 at 13:05
  • Android Version? And tell me which version of Phonegap are you using? I will try it by my self. – Gajotres May 06 '13 at 13:08
  • Android version 4.2.2 and corodova 2.5.0 – Beginner May 06 '13 at 13:13
  • 1
    Gajotres i found out the problem. if i use these lines to align the image to centre, image is getting scrolled `

    ` but if i remove

    tag it is not scrolling. sorry 1st time when you asked i was trying without

    tag and i had posted the same thing. i apologize for it. but if i remove

    tag the image is allinging to left. how will i fix it.

    – Beginner May 06 '13 at 13:42
  • give me a moment and I will find you a solution how to center the image. – Gajotres May 06 '13 at 13:43
  • Try this: http://jsfiddle.net/Gajotres/DDBuP/, I have wrapped img inside another div. – Gajotres May 06 '13 at 13:52
  • If it is not a problem, mail me your project and I will take a look. – Gajotres May 07 '13 at 07:25
  • can you share your mail id? – Beginner May 07 '13 at 08:23
0

I know one of the solutions. It is not perfect but it works really nice. You can specify different css files for each resolution and use mediaqueries. Mediaqueries will replace your css code depending on some rules you can specify, for example depending on screen resolution. It works very well with PhoneGap and jQM. Check it here and read official w3 documentation.

I hope it helps.

Piotr Krysiak
  • 2,815
  • 3
  • 23
  • 35