在固定寬高容器內顯示任意尺寸圖片,要求:等比縮放(不變形)、完整顯示(不裁切)、水平垂直置中。類似 `object-fit: contain` + `object-position: center` 的 CSS 效果,但用 JS 計算兼容舊瀏覽器或特殊佈局需求。
/**
* 圖片等比縮放置中
* @param {HTMLImageElement} img - 圖片元素 (需已載入完成)
* @param {number} boxWidth - 容器寬度
* @param {number} boxHeight - 容器高度
*/
function fitImageCenter(img, boxWidth, boxHeight) {
var imgW = img.naturalWidth || img.width; // 圖片原始寬
var imgH = img.naturalHeight || img.height; // 圖片原始高
if (!imgW || !imgH) return; // 圖片尚未載入
var imgRatio = imgW / imgH;
var boxRatio = boxWidth / boxHeight;
var finalW, finalH, padLeft = 0, padTop = 0;
if (imgRatio > boxRatio) {
// 圖片較寬 → 以寬度為基準縮放
finalW = boxWidth;
finalH = boxWidth / imgRatio;
padTop = (boxHeight - finalH) / 2; // 垂直置中
} else {
// 圖片較高 → 以高度為基準縮放
finalH = boxHeight;
finalW = boxHeight * imgRatio;
padLeft = (boxWidth - finalW) / 2; // 水平置中
}
// 應用樣式
img.style.width = finalW + 'px';
img.style.height = finalH + 'px';
img.style.paddingLeft = padLeft + 'px';
img.style.paddingTop = padTop + 'px';
img.style.boxSizing = 'content-box'; // padding 不計入 width/height
}
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<title>圖片等比縮放置中 Demo</title>
<style>
.img-box {
width: 300px; height: 200px;
border: 2px dashed #ccc;
background: #f9f9f9;
overflow: hidden; /* 防止 padding 撐大容器 */
position: relative;
}
.img-box img {
display: block;
/* 不設 width/height,由 JS 控制 */
}
</style>
</head>
<body>
<h3>原生 JS 等比縮放置中</h3>
<div class="img-box" id="box1">
<img id="img1" src="https://picsum.photos/800/600" alt="橫向圖">
</div>
<div class="img-box" id="box2" style="margin-top:1rem">
<img id="img2" src="https://picsum.photos/400/600" alt="直向圖">
</div>
<div class="img-box" id="box3" style="margin-top:1rem">
<img id="img3" src="https://picsum.photos/300/200" alt="同比例圖">
</div>
<script>
function fitImageCenter(img, boxWidth, boxHeight) {
var imgW = img.naturalWidth || img.width;
var imgH = img.naturalHeight || img.height;
if (!imgW || !imgH) return;
var imgRatio = imgW / imgH;
var boxRatio = boxWidth / boxHeight;
var finalW, finalH, padLeft = 0, padTop = 0;
if (imgRatio > boxRatio) {
finalW = boxWidth;
finalH = boxWidth / imgRatio;
padTop = (boxHeight - finalH) / 2;
} else {
finalH = boxHeight;
finalW = boxHeight * imgRatio;
padLeft = (boxWidth - finalW) / 2;
}
img.style.width = finalW + 'px';
img.style.height = finalH + 'px';
img.style.paddingLeft = padLeft + 'px';
img.style.paddingTop = padTop + 'px';
img.style.boxSizing = 'content-box';
}
// 圖片載入後執行
function init(imgId, boxId) {
var img = document.getElementById(imgId);
var box = document.getElementById(boxId);
var bw = box.clientWidth;
var bh = box.clientHeight;
if (img.complete) {
fitImageCenter(img, bw, bh);
} else {
img.onload = function() { fitImageCenter(img, bw, bh); };
}
// 視窗縮放時重新計算 (若容器響應式)
window.addEventListener('resize', function() {
fitImageCenter(img, box.clientWidth, box.clientHeight);
});
}
init('img1', 'box1');
init('img2', 'box2');
init('img3', 'box3');
</script>
</body>
</html>
.img-box {
width: 300px; height: 200px;
border: 2px dashed #ccc;
background: #f9f9f9;
display: flex;
align-items: center; /* 垂直置中 */
justify-content: center; /* 水平置中 */
}
.img-box img {
max-width: 100%;
max-height: 100%;
/* 等同 object-fit: contain + object-position: center */
}
或更簡潔的 object-fit(IE 不支援,需 Polyfill):
.img-box img {
width: 100%;
height: 100%;
object-fit: contain; /* 等比縮放、完整顯示 */
object-position: center; /* 置中 */
}
object-fit、無 Flexbox)
// 適用於 div 背景圖
function fitBackground(el, url, boxWidth, boxHeight) {
var img = new Image();
img.onload = function() {
var imgW = img.width, imgH = img.height;
var imgRatio = imgW / imgH;
var boxRatio = boxWidth / boxHeight;
var bgSize, bgPos;
if (imgRatio > boxRatio) {
bgSize = boxWidth + 'px auto';
bgPos = 'center top';
} else {
bgSize = 'auto ' + boxHeight + 'px';
bgPos = 'center center';
}
el.style.backgroundImage = 'url(' + url + ')';
el.style.backgroundSize = bgSize;
el.style.backgroundPosition = bgPos;
el.style.backgroundRepeat = 'no-repeat';
};
img.src = url;
}