滑鼠游標移至超連結上時,連結文字顏色以 HSB 色相環循環漸變,產生「彩虹流動」視覺效果;滑鼠移開恢復原色。純 JavaScript 實現,無需外部庫。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>彩虹漸變超連結 Demo</title>
<style>
a:link, a:visited { color:#0066CC; text-decoration:none; }
a:hover, a:active { text-decoration:none; }
.title { font-weight:bold; font-size:13px; color:#ff0000; text-align:center; }
</style>
</head>
<body text="#000000" leftmargin="0">
<div class="title"><a href="https://example.com">三聯網</a></div>
<script>
// === 彩虹漸變核心邏輯 ===
var rate = 20; // 色相遞增步長 (度)
var obj = null; // 目標元素
var act = 0; // 0=停止 1=執行中
var elmH = 0, elmS = 128, elmV = 255; // HSV 初始值
var clrOrg = ''; // 原始顏色備份
var timerID = null;
var isIE = (navigator.appName.indexOf("Microsoft") != -1 && parseInt(navigator.appVersion) >= 4);
if (isIE) {
document.onmouseover = doRainbowAnchor;
document.onmouseout = stopRainbowAnchor;
}
function doRainbowAnchor() {
if (!isIE || act) return;
obj = event.srcElement;
// 向上尋找最近的 標籤
while (obj.tagName != 'A' && obj.tagName != 'BODY') obj = obj.parentElement;
if (obj.tagName == 'A' && obj.href) {
act = 1;
clrOrg = obj.style.color || getComputedStyle(obj).color;
timerID = setInterval(changeColor, 100);
}
}
function stopRainbowAnchor() {
if (!isIE || !act) return;
if (obj.tagName == 'A') {
obj.style.color = clrOrg;
clearInterval(timerID);
act = 0;
}
}
function changeColor() {
obj.style.color = makeColor();
}
function makeColor() {
var r, g, b;
if (elmS == 0) { r = g = b = elmV; }
else {
var t1 = elmV;
var t2 = (255 - elmS) * elmV / 255;
var t3 = (elmH % 60) * (t1 - t2) / 60;
if (elmH < 60) { r = t1; b = t2; g = t2 + t3; }
else if (elmH < 120) { g = t1; b = t2; r = t1 - t3; }
else if (elmH < 180) { g = t1; r = t2; b = t2 + t3; }
else if (elmH < 240) { b = t1; r = t2; g = t1 - t3; }
else if (elmH < 300) { b = t1; g = t2; r = t2 + t3; }
else { r = t1; g = t2; b = t1 - t3; }
}
r = Math.floor(r); g = Math.floor(g); b = Math.floor(b);
var hex = '#' + r.toString(16).padStart(2,'0') + g.toString(16).padStart(2,'0') + b.toString(16).padStart(2,'0');
elmH = (elmH + rate) % 360;
return hex;
}
</script>
</body>
</html>
const rate = 20;
let hue = 0, saturation = 128, value = 255;
let targetEl = null, originalColor = '', timerId = null;
function hsvToHex(h, s, v) {
s /= 255; v /= 255;
const c = v * s;
const x = c * (1 - Math.abs((h / 60) % 2 - 1));
const m = v - c;
let r=0,g=0,b=0;
if (h < 60) [r,g,b] = [c,x,0];
else if (h < 120) [r,g,b] = [x,c,0];
else if (h < 180) [r,g,b] = [0,c,x];
else if (h < 240) [r,g,b] = [0,x,c];
else if (h < 300) [r,g,b] = [x,0,c];
else [r,g,b] = [c,0,x];
r = Math.round((r+m)*255); g = Math.round((g+m)*255); b = Math.round((b+m)*255);
return '#' + [r,g,b].map(c => c.toString(16).padStart(2,'0')).join('');
}
document.addEventListener('mouseover', e => {
const a = e.target.closest('a[href]');
if (a && !timerId) {
targetEl = a;
originalColor = getComputedStyle(a).color;
timerId = setInterval(() => {
targetEl.style.color = hsvToHex(hue, saturation, value);
hue = (hue + rate) % 360;
}, 100);
}
});
document.addEventListener('mouseout', e => {
if (targetEl && !e.target.closest('a[href]')) {
targetEl.style.color = originalColor;
clearInterval(timerId);
timerId = null;
targetEl = null;
}
});