Search code examples
javascriptjquerymobilecordovascale

Javascript mobile - calculate element dimensions in pixels based on screen dimensions


I want to calculate the dimensions of certain elements (img, ul, div, etc.) based on screen size. I can't to use percent values. I need pixel values. I also don't want to 'hardcode' everything using media queries and a new set of images for every resolution or screen size.

I thought about making this using screen size. I only need width calculation. So I add the initial width of my images and some initial space between them -> total width, and I then get scaling factor using: screenwidth / totalwidth

Now I scale all of my images and also the space between with this factor.

It's a very simple layout, only a few images and HTML elements. So this scaling should not be expensive.

It would work if the devices gave me reliable width measure for the screen. But depending of the device, I get a different meaning of this value. I'm using screen.width

In some cases screen.width is what the currently width is - in portrait it's a small value, in landscape a large one. But in other ones, width is always the same - the value which is defined as device's width.

So how do I scale my layout according to what's currently screen width in a consistent way with rotation, and without CSS % values? Is this an acceptable way to do layout scaling or am doing no-go?

Edit: I have to add more details after trying Jasper's solution. The images are used in a slider. The slider is basically an UL and each LI contains an image with float:left - so all the images are appended horizontally one after the other. With overflow hidden and stuff only the current slide is visible. Now, the official width of the UL is the sum of the width of all contained LIs. And this means, at least with my current state of knowledge, that I can't use percentage size for the LI elements, because if I did, this will be % of this total width of the UL, which is very large, and I end with immense LI elements/images.

Isn't there any reliable way to get current screen width for all devices ? I already have working code, I only need that the value of screen width is correct.

New update

Look here is a similar approach to what I'm trying to do:

http://ryangillespie.com/phonegap.php#/phonegap.php?

Entry of June 18, 2011 "One Screen Resolution to Rule Them All"

I tried also with exactly that example, copy pasting it in my code. But it doesn't work either. window.outerWidth has the same problems as I'm describing for screen.width (as well as JQuery $('body').width()). It works as long as the device isn't rotated - it initializes well. But at the first rotation, depending of the device, I get problems. In some it works as expected, in others it interchanges the values, so that I get large width in portrait mode and short in landscape, in others it gives fixed width and height all time, in others it doesn't rotate at all....


Solution

  • Coincidentally I found that this works:

    $(window).resize(function() {
        updateScaling($('body').width());
    }); 
    

    This is always called and passes correct width. As far as I remember it also works with screen.width

    In updateScaling I calculate a scalingFactor and adjust my elements.

    I tried out responsive CSS, media queries and so on, but at some point it didn't make sense anymore, because I have anyways to recalculate the margin of slider's UL based on current slide and new width - and other stuff which needs script. So I made everything with script.

    I removed window.onorientationchange.