本文分享一個視覺上相當有意思的排列效果:一排圖片整齊陳列,當滑鼠移到其中一張時,該張放大並高亮,而左右鄰居則依距離遞減地縮小,形成一道對稱的波浪。因為這種「中心脹、兩側消」的節奏感,看起來就像手指劃過鋼琴鍵盤。
運行效果如下:

小提醒:若瀏覽器無法正常顯示,可嘗試切換相容模式。
另外有一個容易忽略的細節:圖片的 alt 屬性不可省略,因為程式會從 alt 讀取資料,缺少它效果就會失效。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JS 鋼琴按鈕波浪式圖片排列效果</title>
<style>
#bookrack {
width: 1200px;
margin: 100px auto;
overflow: hidden;
}
#bookrack a {
text-align: center;
text-decoration: none;
font-size: 12px;
}
#bookrack span {
display: none;
position: absolute;
color: #fff;
background-color: #000;
background: rgba(0, 0, 0, 0.5);
padding: 5px;
top: 30px;
left: 0;
}
#bookrack span big { color: red; font-size: 14px; }
#bookrack img {
width: 100%;
height: 100%;
display: block;
border: 0;
}
</style>
</head>
<body>
<div id="bookrack">
<a href="#" title=""><img src="images/2.jpg" alt="" /></a>
<a href="#" title=""><img src="images/3.jpg" alt="" /></a>
<a href="#" title=""><img src="images/4.jpg" alt="" /></a>
<a href="#" title=""><img src="images/5.jpg" alt="" /></a>
<a href="#" title=""><img src="images/6.jpg" alt="" /></a>
<a href="#" title=""><img src="images/7.jpg" alt="" /></a>
<a href="#" title=""><img src="images/8.jpg" alt="" /></a>
<a href="#" title=""><img src="images/2.jpg" alt="" /></a>
<a href="#" title=""><img src="images/3.jpg" alt="" /></a>
<a href="#" title=""><img src="images/4.jpg" alt="" /></a>
<a href="#" title=""><img src="images/5.jpg" alt="" /></a>
<a href="#" title=""><img src="images/6.jpg" alt="" /></a>
<a href="#" title=""><img src="images/7.jpg" alt="" /></a>
<a href="#" title=""><img src="images/8.jpg" alt="" /></a>
</div>
<script>
var Bookrack = function(a, b, c, e) {
this.scale = e || 0.1; // 縮放比例
this.x = b || 120; // 單項基準寬
this.y = c || 160; // 單項基準高
this.border = 2;
this.init(a);
this.exec(Math.ceil(Math.random() * this.imgs.length));
};
Bookrack.prototype = {
init: function(a) {
this.width = a.clientWidth - 2 * this.x * this.scale;
a.style.position = "relative";
a.style.height = this.y + "px";
this.imgs = a.getElementsByTagName("a");
var b = this,
c = document.createElement("span"),
e, d;
this.each(function(a, g) {
a.style.position = "absolute";
a.style.bottom = "0";
a.style.border = this.border + "px solid gray";
// 依索引均勻分佈水平位置
a.style.left = this.width * (g / this.imgs.length) + 2 * this.border + "px";
a.setAttribute("dir", g);
// 為每個項目準備一個 tooltip 容器
d = c.cloneNode(!0);
d.innerHTML = a.getAttribute("title");
a.appendChild(d);
a.onmouseover = function() {
b.exec(this.getAttribute("dir"));
};
});
},
each: function(a) {
for (var b = 0, c; c = this.imgs[b++];) {
a.call(this, c, b, this.imgs.length);
}
},
// 把 0~1 的比率轉成灰階色碼
color: function(a) {
a = (~~(255 * a)).toString(16);
2 > a.length && (a = "0" + a);
a = a.substr(0, 2);
return "#" + a + a + a;
},
exec: function(a) {
this.each(function(b, c, e, d, f) {
b.getElementsByTagName("span")[0].style.display = "none";
c == a && (b.getElementsByTagName("span")[0].style.display = "block");
// 以比例取正弦,得到 0~1 的平滑衰減係數
d = Math.min(c / a, a / c);
f = Math.sin(90 * (Math.PI / 180) * d) * (1 - this.scale);
b.style.zIndex = Math.ceil(1E4 * f);
b.style.borderColor = this.color(f + this.scale);
b.style.width = this.x * (f + this.scale) - 2 * this.border + "px";
b.style.height = this.y * (f + this.scale) - 2 * this.border + "px";
b.style.marginLeft = this.x * f / -2 + "px";
});
}
};
new Bookrack(document.getElementById('bookrack'), 120, 160);
</script>
</body>
</html>
整個效果的核心是 exec() 裡那兩行:
d = Math.min(c / a, a / c); f = Math.sin(90 * (Math.PI / 180) * d) * (1 - this.scale);
a 是當前滑鼠所在項目的索引,c 是正在計算的項目索引。c / a 與 a / c 取較小值,可確保無論滑鼠指向左半邊還是右半邊,係數都落在 0 到 1 之間,且離滑鼠越遠越小。接著把這個比例送進正弦函式,得到一條平滑的衰減曲線——這正是「波浪」而非「階梯」的來源。
係數 f 隨後被同時套用於四處:寬高(x * (f + scale))、z-index(決定前後遮擋關係)、邊框顏色(透過 color() 轉成灰階,越亮代表越靠前),以及 marginLeft(用來讓縮放以中心為基準而非左上角)。四者同步變化,才有立體的膨脹感。
each() 是作者自製的迭代器,把索引與總數一起回傳給 callback,讓 init 與 exec 都能用同一套寫法遍歷節點。整個元件用建構函式加 prototype 組織,初始參數(單項寬高、縮放比例)可外部傳入,具備基本的可重用性。