私はOliverBoermansのjQueryプラグインを使用して、画像を100%の幅と高さ(つまり、基本的にはビューポート)のコンテナーに適合させています。http://www.ollicle.com/eg/jquery/imagefit/
プラグインの要点はこれです。
$(img)
.width('100%').each(function()
{
$(this).height(Math.round(
$(this).attr('startheight')*(dim.outerWidth/$(this).attr('startwidth')))
);
})
.css({marginTop: Math.round( ($(window).height() - $(img).height()) / 2 )});
startwidth
andstartheight
属性には、画像の実際のピクセル寸法が含まれます。
これで、画像がビューポートよりも広い場合、これは完全に機能します。ただし、ビューポートが画像よりも垂直方向に短い場合、画像がビューポートに垂直方向に収まらないことがわかりました。
このプラグインを変更して、水平方向に加えて垂直方向のフィッティングを考慮に入れて、ビューポートの寸法とアスペクト比が何であれ、画像のすべてのインチが常に完全に表示されるようにするにはどうすればよいですか?
遊ぶためのフィドルを用意しました。http://jsfiddle.net/NXJCd/-プラグインが動作していることを確認するために、結果部門のサイズを調整します。フィットした画像よりも高さを短く設定すると、画像の上端と下端がカットされます。
編集:それで、いくつかの混乱の後、私は明確にしたいです。画像をコンテナ内に比例的にフィットさせたい。すべての画像は常に表示される必要がありますが、元のサイズより大きくなることはありません。以下に視覚化しました。このために私が持っているjQueryプラグインは水平方向のフィッティング(例AとC)で機能しますが、垂直方向のフィッティングではありません(例B)。それが私が解決したいことです。
EDIT2:さらに混乱した後、私はそれがどのように動作するかを詳細に説明するアニメーションを作成しました。http://www.swfme.com/view/1064342