後台上傳的圖片尺寸五花八門,直接塞進固定寬高的容器裡往往會被拉伸變形。下面這個方案的核心思路是:等比縮放圖片到恰好鋪滿容器,多出來的部分通過位移動畫在容器內滑動展示,因此無論原圖是橫圖還是豎圖,都不會變形。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>任意尺寸的圖片滑動</title>
<style>
div { margin: 0 auto; overflow: hidden; }
.main { width: 1000px; }
.divimg_div1 { width: 380px; float: left; }
.divimg .div4_title {
width: 380px; height: 103px; background-color: #EDB205; color: #FFF;
font-family: "微軟雅黑"; font-size: 22px; font-weight: bold;
line-height: 90px; text-align: center; letter-spacing: 5px;
}
.divimg_img1 { width: 380px; height: 414px; margin-top: 5px; background-color: #FFF; position: relative; }
.divimg_div2 { width: 615px; float: right; }
.divimg_img2 { width: 194px; height: 256px; float: left; background-color: #FFF; position: relative; }
.divimg_img3 { width: 417px; height: 256px; float: right; background-color: #FFF; position: relative; }
.divimg_img4 { width: 366px; height: 262px; float: left; margin-top: 4px; background-color: #FFF; position: relative; }
.divimg_img5 { width: 245px; height: 262px; float: right; margin-top: 4px; background-color: #FFF; position: relative; }
.divimg .gif { position: absolute; left: 50%; top: 50%; margin-left: -50px; margin-top: -50px; z-index: 2; }
.divimg .img { position: absolute; z-index: 1; left: 0; top: 0; display: none; }
</style>
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function (e) {
$(".divimg .img").load(function () {
var w = parseInt($(this).width());
var h = parseInt($(this).height());
var hh = $(this).parent().height();
var ww = $(this).parent().width();
var blw = w / parseInt(ww);
var blh = h / parseInt(hh);
function left() { $(this).animate({ left: -(parseInt(parseInt(hh) / h * w) - parseInt(ww)) }, 10000, right); }
function right() { $(this).animate({ left: 0 }, 10000, left); }
function top() { $(this).animate({ top: -(parseInt(parseInt(ww) / w * h) - parseInt(hh)) }, 10000, bottom); }
function bottom() { $(this).animate({ top: 0 }, 10000, top); }
if (blw > blh) {
// 圖片相對更寬:以容器高度為基準縮放,左右來回滑動
$(this).height(hh).width(parseInt(parseInt(hh) / h * w));
$(this).prev().hide();
$(this).css({ "z-index": "3", "display": "block" });
$(this).animate({ left: -(parseInt(parseInt(hh) / h * w) - parseInt(ww)) }, 10000, right);
} else if (blw < blh) {
// 圖片相對更高:以容器寬度為基準縮放,上下來回滑動
$(this).height(parseInt(parseInt(ww) / w * h)).width(ww);
$(this).prev().hide();
$(this).css({ "z-index": "3", "display": "block" });
$(this).animate({ top: -(parseInt(parseInt(ww) / w * h) - parseInt(hh)) }, 10000, bottom);
}
});
$(".div4 .img").each(function (index, element) {
$(this).attr("src", $(this).attr("name"));
});
});
</script>
</head>
<body>
<div class="main">
<div class="divimg">
<div class="divimg_div1">
<div class="divimg_title">任意尺寸的圖片滑動</div>
<div class="divimg_img1">
<img class="gif" src="images/loadding.gif" width="100" height="100" />
<img class="img" name="images/am_bigimg01.jpg" src="" />
</div>
</div>
<div class="divimg_div2">
<div class="divimg_img2">
<img class="gif" src="images/loadding.gif" width="100" height="100" />
<img class="img" name="images/am_nyimg01.jpg" src="" />
</div>
<div class="divimg_img3">
<img class="gif" src="images/loadding.gif" width="100" height="100" />
<img class="img" name="images/am_img02.jpg" src="" />
</div>
<div class="divimg_img4">
<img class="gif" src="images/loadding.gif" width="100" height="100" />
<img class="img" name="images/am_newimg01.jpg" src="" />
</div>
<div class="divimg_img5">
<img class="gif" src="images/loadding.gif" width="100" height="100" />
<img class="img" name="images/am_rynewimg03.jpg" src="" />
</div>
</div>
</div>
</div>
</body>
</html>
先用 loading 佔位。每個容器裡放一張 gif 佔位圖,真實圖片寫在 name 屬性而非 src 上,等文檔就緒後再統一賦值。這樣可以避開 src 為空導致的重複請求,也讓加載過程有明確的視覺反饋。
在 load 而非 ready 中計算尺寸。只有圖片真正加載完成,width() 與 height() 才是可靠的。若在 ready 階段讀取,拿到的往往是 0。
用比例判斷滑動方向。分別計算圖片寬高相對於容器寬高的縮放比 blw 與 blh。若 blw 更大,說明圖片偏橫,按容器高度等比縮放後寬度溢出,於是左右平移;反之則按容器寬度縮放,上下平移。
動畫函數互相調用形成循環。left 與 right、top 與 bottom 彼此作為對方的最終回調,構成一個往復的無限動畫。
如果是在新項目中使用,用 CSS 的 object-fit: cover 搭配 object-position 動畫,可以省掉幾十行 JS 計算;想讓圖片平滑漫遊,也可以改用 CSS animation 的 keyframes 來替代 jQuery animate,性能會更好。