私は、ウィンドウのサイズに合わせて画像のサイズを調整する簡単な機能を作りました。私はいつ正確に判断することはできませんが、時にはimgが画面の幅を埋めることはありませんが、高さに固執し続けます。なぜこれができるのか?フルスクリーン&中心の背景画像スクリプト
iコンソールログ(iRatio < = wRatio)のいずれかが適合しているようですが、表示された結果が正しくありません。
imgは、CSSでpostion: absolute; with: 100%; top:0; left:0;
と設定されています。あなたはそれが親の幅をいっぱいになります100%に画像の幅を設定している場合 $win
は$(window)
と$img
背景画像
function autoImageSize($img, $win){
var wHeight = $win.height(),
wWidth = $win.width(),
iHeight = $img.height(),
iWidth = $img.width(),
iRatio = iWidth/iHeight,
wRatio = wWidth/wHeight;
if(iRatio <= wRatio){
$img.css({
width: "100%",
height: "auto",
top: "-" + ((iHeight - wHeight)/2) + "px",
left: 0
});
}else{
$img.css({
width: "auto",
height: "100%",
top: 0,
left: "-" + ((iWidth - wWidth)/2) + "px"
});
}
return [$img.width(), $img.height()];
};
私は、あなたが背景イメージを意味すると思います._ –
ありがとうございます。私はそれを修正しました – meo