【发布时间】:2015-08-28 17:44:52
【问题描述】:
我正在尝试寻找一种更好的方法来调整顶部横幅图像的大小,创建适合视口方向和视口大小的不同图像,而不会“剪切”或扭曲图像。我创建了以下代码(css + js),我想知道是否是正确的方法,或者是否存在更好的方法。
CSS 到 <img> 或 <div> 与 background-image:
max-width: 100%; width: auto; height: auto;
JS:
$(document).ready(function () {
resizeViewPort();
});
$(window).resize(function () {
resizeViewPort();
});
function resizeViewPort() {
alert(($(window).height() / $(window).width()).toFixed(2));
var height = $(window).height(), width = $(window).width();
var scale = (height / width).toFixed(2);
if (scale >= 1.7 && scale <= 1.8) {
if (height <= 736) { // 736x414
alert("Mobile PORTRAIT");
}
}
else if (scale >= 1.3 && scale <= 1.6) {
if (height <= 1024) { // 1024x768
alert("Tablet PORTRAIT");
}
}
else if (scale >= 0.6 && scale <= 0.8) {
if (height <= 768) { // 768x1024
alert("Tablet LANDSCAPE or Desktop 4:3");
} else if (height <= 900) { // 900x1440
alert("Desktop HD 16:10");
} else if (height <= 1200) { // 1200x1920
alert("Desktop FullHD 16:10");
}
}
else if (scale >= 0.5 && scale < 0.6) {
if (height <= 414) { // 414x736
alert("Mobile LANDSCAPE");
} else if (height <= 720) { // 720x1280
alert("Desktop HD 16:9");
} else if (height <= 1080) { // 1080x1920
alert("Desktop FullHD 16:9");
}
}
}
【问题讨论】:
-
想要的效果是什么?保持纵横比?调整到固定比例?目前尚不清楚您想要实现什么。
-
@JosephMarikle 我想创建适合视口方向和视口大小的不同图像,而不会“剪切”或扭曲图像。
标签: javascript html css image resize