利用 40 個 <td> 單元格組成一行,通過 JavaScript 週期性修改每個單元格背景色,形成一條會流動的七彩橫線分割條。屬於早期「網頁特效」典型範例。
document.write 寫入一個 width=100%、height=4 的表格,包含 40 個 id="a_mo0" ~ "a_mo39" 的單元格。l = [6,7,8,9,'a','b','b','c','d','e','f'] 對應十六進制色值片段。f1(y) 每 1ms 執行一次,根據索引計算色值 #00 + l[c] + l[c] + 00(藍綠漸變),並對 40 個單元格依次偏移賦色,形成流動感。l = Array(6,7,8,9,'a','b','b','c','d','e','f');
t = "<table height=4 width=100% cellspacing=0 cellpadding=0><tr>";
for (x = 0; x < 40; x++) {
t += "<td id=a_mo" + x + "></td>";
}
document.write(t + "</tr></table>");
function f1(y) {
for (i = 0; i < 40; i++) {
c = (i + y) % 20;
if (c > 10) c = 20 - c;
document.all["a_mo" + i].bgColor = "'#00" + l[c] + l[c] + "00'";
}
y++;
setTimeout('f1(' + y + ')', 1);
}
f1(1);
.rainbow-hr {
height: 4px;
background: linear-gradient(90deg, #006600, #00aa00, #00ff00, #00aa00, #006600);
background-size: 500% 100%;
animation: flow 2s linear infinite;
}
@keyframes flow {
0% { background-position: 0% 50%; }
100% { background-position: 100% 50%; }
}
僅需一個 <div class="rainbow-hr"></div>,零 JS、GPU 加速、響應式完美適配。