スクロールで色と形が変わるヘッダー
背景色自体が虹色で、それがさらに波打つように動くスタイルです。
HTML / CSS
CSS
.rainbow-header {
position: fixed;
top: 0;
width: 100%;
height: 70px;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 5%;
box-sizing: border-box;
color: white;
z-index: 9999;
/* 虹色グラデーション */
background: linear-gradient(45deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000);
background-size: 400% 400%;
animation: rainbowAnimation 8s ease infinite;
}
@keyframes rainbowAnimation {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* 文字を読みやすくするための装飾 */
.logo { font-weight: 900; font-size: 1.5rem; letter-spacing: 2px; }
nav a { color: white; text-decoration: none; margin-left: 20px; font-weight: bold; }
JavaScript
JavaScript
const header = document.getElementById('main-header');
header.addEventListener('mousemove', (e) => {
// マウスのX座標によってアニメーションの速度を変える
const speed = (e.clientX / window.innerWidth) * 5 + 1;
header.style.animationDuration = `${speed}s`;
// マウスの位置に微妙に光を当てる(CSS変数を使用)
const x = e.clientX - header.offsetLeft;
const y = e.clientY - header.offsetTop;
header.style.background = `radial-gradient(circle at ${x}px ${y}px, rgba(255,255,255,0.3) 0%, transparent 50%),
linear-gradient(45deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000)`;
header.style.backgroundSize = "400% 400%";
});
// マウスが離れたら速度を戻す
header.addEventListener('mouseleave', () => {
header.style.animationDuration = '8s';
header.style.background = ""; // 元のCSSに戻る
header.style.backgroundSize = "400% 400%";
});
とのことだったので、コードを張り付けてみる
しかし、我がホームページ(というかCocoon)の場合、ヘッダー付近は
| <div id=”header-container” class=”header-container”> |
| <div id=”header-container-in” class=”header-container-in hlt-center-logo-top-menu cl-slim”> |
| <header id=”header” class=”header cf” itemscope itemtype=”https://schema.org/WPHeader”> |
| <div id=”header-in” class=”header-in wrap cf” itemscope itemtype=”https://schema.org/WebSite”> |
となっている。どこにこの設定を追加したらいいのか・・・。

ちなみに現在のヘッダー。
<div id=”header-container” class=”header-container”>部分に設定してみる
このクラスにCSSとJavaScriptを変える必要がある。今回のCSSでいえば
.rainbow-header の部分を .header-container に、JavaScriptは
main-header を header-container
に変える必要がある。

派手ぇ!
というか脳に悪い。試しにヘッダーにカーソルを持っていくとポリゴン現象みたいなのが起こる。
あと配置が中心からずれてしまっているのは気になる。つまりポジション系はいじらない方がよさそう。
つまりいろいろ直してこれが答えのCSSだ!
.header-container {
/* 虹色グラデーション */
background: linear-gradient(45deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000);
background-size: 400% 400%;
animation: rainbowAnimation 8s ease infinite;
}
@keyframes rainbowAnimation {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* 文字を読みやすくするための装飾 */
.site-name-text { font-weight: 900; font-size: 1.5rem; letter-spacing: 2px; }
nav a { color: white; text-decoration: none; margin-left: 20px; font-weight: bold; }
/* ★光専用のレイヤー */
.header-container::before {
content: '';
position: absolute;
top: 0; left: 0; width: 100%; height: 100%;
/* JSから渡される座標を使う */
background: radial-gradient(circle 150px at var(--mouse-x, -1000px) var(--mouse-y, -1000px), rgba(255, 255, 255, 0.6), transparent);
pointer-events: none; /* マウスイベントを邪魔しない */
z-index: 1;
}
/* 文字が光の下に隠れないように */
.site-name-text, nav {
position: relative;
z-index: 2;
}
JavaScript
const header = document.getElementById('header-container');
header.addEventListener('mousemove', (e) => {
const rect = header.getBoundingClientRect();
// ヘッダー内の相対座標を計算
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// CSS変数(カスタムプロパティ)だけを更新
header.style.setProperty('--mouse-x', `${x}px`);
header.style.setProperty('--mouse-y', `${y}px`);
});
このコードを貼るだけでド派手で白いエフェクトがマウスに付くおしゃれ?なヘッダーが完成だ!


コメント