我有什么?
- Web 应用程序(仅 html、css、js),应用程序具有固定的宽度和高度(1024*768);
- 通过 Web 应用程序使用 cordova 创建的 Android 应用程序;
- 在 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/