Search code examples
javascriptandroidhtmlcordovameta

Need to fit application width to device width


What do i have?

  1. Web app(just html,css,js), application has fixed width and height (1024*768);
  2. Android application created with cordova by web application;
  3. Testing android application on htc desire 500 (android version 4.1.2)

Problem: I need width of application fit to device width on all android devices. Now my application is bigger then device width.

What i tried to do?

Changed viewport meta tag in different ways like

<meta name="viewport" content="width=device-width, user-scalable=no" />

Used javascript to change app zoom (on deviceredy event)

var contentWidth = document.body.scrollWidth, 
    windowWidth = window.innerWidth, 
    newScale = windowWidth / contentWidth;
    document.body.style.zoom = newScale;

Used javascript to change viewport

var ww = (jQuery(window).width() < window.screen.width) ?
    jQuery(window).width() : window.screen.width;
// min width of site
var mw = 1020;
//calculate ratio
var ratio =  ww / mw;
if (ww < mw) {
    jQuery('meta[type="viewport"]').attr('content', 'initial-scale=' + ratio + ', maximum-scale=' + ratio + ', minimum-scale=' + ratio + ', user-scalable=yes, width=' + ww);
} else {
    jQuery('meta[type="viewport"]').attr('content', 'initial-scale=1.0, maximum-scale=2, minimum-scale=1.0, user-scalable=yes, width=' + ww);
}

But solutions that i describe above can`t help. Do anyone knows solution that can help do that i need?


Solution

  • I have solved problem by using css transform, this css property is similar to zoom but it is supported by all modern browsers. Notice: This solution appropriate just for fixed size layouts

    Function that i wrote to solve my problem (Just put body as element, and it initial size as height and width):

    /**
     * @param {string} element - element to apply zooming
     * @param {int} width - initial element width
     * @param {int} height - initial element heigth
     * @param {float} desiredZoom - which zoom you need, default value = 1
     */    
    function zoomElement(element, width, height, desiredZoom) {
            desiredZoom = desiredZoom || 1;
            var zoomX = (jQuery(window).width() * desiredZoom) / width,
                zoomY = (jQuery(window).height() * desiredZoom) / height,
                zoom = (zoomX < zoomY) ? zoomX : zoomY;
    
            jQuery(element)
                .css('transform', 'scale(' + zoom + ')')
                .css('transform-origin', '0 0')
                // Internet Explorer
                .css('-ms-transform', 'scale(' + zoom + ')')
                .css('-ms-transform-origin', '0 0')
                // Firefox
                .css('-moz-transform', 'scale(' + zoom + ')')
                .css('-moz-transform-origin', '0 0')
                // Opera
                .css('-o-transform', 'scale(' + zoom + ')')
                .css('-o-transform-origin', '0 0')
                // WebKit
                .css('-webkit-transform', 'scale(' + zoom + ')')
                .css('-webkit-transform-origin', '0 0');
    
            // Center element in it`s parent
            (function () {
                var $element = jQuery(element);
    
                // Remove previous timeout
                if ($element.data('center.timeout')) {
                    console.log("clear timeout");
                    clearTimeout($element.data('center.timeout'));
                }
    
                var timeout = setTimeout(function () {
                    $element.data('center.timeout', timeout);
                    var parentSize = {
                            'height' : $element.parent().height(),
                            'width' : $element.parent().width()
                        },
                        rect = element.get(0).getBoundingClientRect();
    
                    element.css({
                        'marginTop' : 0,
                        'marginLeft' : 0
                    });
    
                    // Center vertically
                    if (parentSize.height > rect.height) {
                        var marginTop = (parentSize.height - rect.height) / 2;
                        element.css('marginTop', marginTop);
                    }
    
                    // Center horizontally
                    if (parentSize.width > rect.width) {
                        var marginLeft = (parentSize.width - rect.width) / 2;
                        element.css('marginLeft', marginLeft);
                    }
                }, 300);
            })();
        }