Phonegap - Reliable Page Width Detection?
Can anybody recommend a good way to detect screen width on a HTML5 Android App built in Phonegap? Our page is reasonably fluid - but I need to switch logo's/footer graphics for sma
Solution 1:
You don't need JavaScript to add additional rules based on your screen dimensions, you can use CSS3 Media Queries instead like:
.smallScreenContent{
display: none;
}
@media (max-width:400px){ //the following rules will only be used for screens up to 400px width.smallScreenContent{
display: block;
}
}
See this link for specifications and this fiddle (resize the result) for a working demo.
Solution 2:
The only way I found to correctly detect the window size is to put a timeout of a couple of seconds before to let the canvas fit the screen. At launch it shows up on the top left corner and only spreads to fit the screen after a short while.
Solution 3:
Use
$(window).width() // For width
$(window).height() // For Height
Alternatively you could check the width of a data-role="page"
element to find the device-width
(since it's set to 100%
of the device-width
):
var deviceWidth = 0;
$(window).bind('resize', function () {
deviceWidth = $('[data-role="page"]').first().width());
}).trigger('resize');
Post a Comment for "Phonegap - Reliable Page Width Detection?"