javascript - 需要使应用程序宽度适合设备宽度

标签 javascript android html cordova meta

我有什么?

  1. Web 应用程序(仅 html、css、js),应用程序具有固定的宽度和高度(1024*768);
  2. 通过 Web 应用程序使用 cordova 创建的 Android 应用程序;
  3. 在 htcdesire 500(Android 版本 4.1.2)上测试 Android 应用程序

问题: 我需要应用程序的宽度适合所有 Android 设备上的设备宽度。现在我的应用程序比设备宽度更大。

我想做什么?

以不同方式更改视口(viewport)元标记,例如

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

使用 JavaScript 更改应用程序缩放(在 deviceredy 事件上)

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

使用 javascript 更改视口(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);
}

但是我上面描述的解决方案没有帮助。有谁知道可以帮助实现我需要的解决方案?

最佳答案

我已经通过使用CSS变换解决了问题,这个CSS属性类似于缩放,但所有现代浏览器都支持它。 注意:此解决方案仅适用于固定尺寸布局

我为解决问题而编写的函数(只需将 body 作为元素,并将其初始大小作为高度和宽度):

/**
 * @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);
        })();
    }

关于javascript - 需要使应用程序宽度适合设备宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28111625/

相关文章:

javascript - 为什么我的 li 被忽略了?

javascript - 如何将数组argu从object_c转换为javascript?

javascript - CoffeeScript 数组查找下一个位置

javascript - 当编辑器在对话框之前创建时,CKEditor 4 在 jquery 对话框中损坏

android - Webview 上的 Telegram URL(在 Telegram 应用程序链接上共享)

javascript - 如何根据浏览器选择 `window.URL.createObjectURL()`和 `window.webkitURL.createObjectURL()`

html - 如何在两个等高的列中放置 3 个图像?

javascript - 获取所选选项的内部 html

android - 摩托罗拉 TC55 拒绝出现在 adb 中

android - listfragment 内的多个 listview