I'm trying to make a generic javascript dialog-class based on JQuery to create div-popups centered on the screen. Achieving this in all the common browsers was plain vanilla.
For mobile platforms, the issue of the viewport arises; the difference of the visible viewport (which is your current "viewing window" of the site as you see it, zoomed in or not) and the layout viewport (the dimensions of the underlying page, or in other words, the CSS viewport).
For Iphone, I have been able to use the DOM property window.innerWidth and window.innerHeight to adjust centering for the scaling, and pageXOffset / pageYOffset to adjust for panning, with:
// Get dialog width/height
var dx = $("#dialog").width();
var dy = $("#dialog").height();
// Get window (layout viewport) width/height
var winW = $(window).width();
var winH = $(window).height();
if (window.innerWidth!=winW) {
// Zoomed in or users browser window width is smaller than layout width
var x = window.pageXOffset+(window.innerWidth-dx)/2;
} else {
// Not zoomed in
var x = window.pageXOffset+(winW-dx)/2;
}
if (window.innerHeight!=winH) {
// Zoomed in or users browser window height is smaller than layout height
var y = window.pageYOffset+(window.innerHeight-dy)/2;
} else {
// Not zoomed in
var y = window.pageYOffset+(winH-dy)/2;
}
I then position my dialog by setting it's left/top to x and y respectively. This works well on most browsers and even the Iphone, it does however not work on Android platforms.
After doing some excessive research using Google, it seems that Android has quite some issues with the window.innerWidth and window.innerHeight properties (see f eg http://www.quirksmode.org/mobile/viewports2.html , search for "Measuring the visible viewport").
An option would be to use the useragent in order to identify Android browsers and always position the dialog at window.pageXOffset / window.pageYOffset, which would always position them top/left in the visible viewport. However, this is a bad option for many reasons, not least that it looks bad when zoomed-out.
Does anyone know of a method to calculate the visible viewport on Android? Or has anyone found a workaround for this?
Without a viewport meta tag, mobile devices render pages at typical desktop screen widths and then scale the pages down, making them difficult to read. Setting the viewport meta tag lets you control the width and scaling of the viewport so that it's sized correctly on all devices.
Go to your website page from your computer, press Ctrl+U, and then find “viewport” (Ctrl+F). If you did not find a viewport meta tag, then you need to add it. Go to this page from your smartphone and see how it looks there in the browser.
What does “The initial scale in the viewport meta tag is not working” mean? This means that the URL in question does not contain the initial scale in the HTML document. Or, this element is on the page, but you set it to a value other than 1.0.
The viewport meta tag is an important component of responsive web design that may also provide some performance benefits.
This is an old question, but I couldn't find a good answer to this... So after a bunch of work on a bunch of Android devices I think I found a great solution (hack) to the Android issue. Try this:
<!--HTML VERSION -->
<div id="sojernTest" style="position:absolute; top:0;bottom:0;left:0;right:0;display:none;"></div>
OR
// JAVASCRIPT WITH CSS CLASS
var div = document.createElement('div');
div.id = "screenSizeHolder";
div.className = "screen_size_holder";
document.body.insertBefore(div, document.body.firstChild);
$('#screenSizeHolder').html(" ");
Then grab the size of that element
screenHeight = parseInt($('#screenSizeHolder').css('height').replace('px',''));
screenWidth = parseInt($('#screenSizeHolder').css('width').replace('px',''));
The answer seems to be that you have to set the viewport width to device-width. It seems to work in all the Android versions I have tested (2.1, 2.2 and 2.3).
<meta name="viewport" content="width=device-width">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With