我试图在一个数组中找到最接近另一个(用户的浏览器高度)并且h
是随机的数字-Infinity
。这是我的代码。
function match_height() {
var heights = [11, 630, 693, 756, 819, 882, 945, 1008, 1071, 1134, 1197, 1260, 1323, 1386, 1449, 1512, 1575, 1638, 1701, 1764, 1827, 1890, 1953, 2016, 2079, 2142, 2205, 2268, 2331, 2394, 2457, 2520];
var posb = [];
var browsery = $(window).height();
for (var i = 0; i < heights.length; i++) {
if (browsery > heights[i] && heights[i + 1] <= browsery) {
posb.push(heights[i+1]);
}
}
var h = Math.max.apply(Math, posb);
$(".area").css("height", h + "px");
$(".area").css("width", "100%");
$(".ground").css("background", "url(scenes/ground/" + h + ".png)");
}
match_height();