【发布时间】:2011-07-20 22:50:05
【问题描述】:
我正在尝试编写一个 JavaScript 函数,该函数将扩展图像以始终填充 div(因此根据需要裁剪顶部或侧面)。它是 CSS3 代码 background-size:cover 的 JavaScript 等价物。
我这辈子都想不通。这是我目前所拥有的:
function full_bleed(box_width, box_height, new_width, new_height)
{
var aspect_ratio=new_width/new_height;
if(new_height<box_height) {
new_height=box_height;
new_width=Math.round(new_height*aspect_ratio);
}
if(new_width<box_width) {
new_width=box_width;
new_height=Math.round(new_width/aspect_ratio);
}
return {
width: new_width,
height: new_height
};
}
我想你们中的一个人可能有这个等式。
【问题讨论】:
-
你应该查看 bgStretcher 的来源(或者直接使用它),它本质上就是这样做的(虽然与整个窗口相关联)ajaxblender.com/…
标签: javascript resize crop