3Dに傾く透過カードのCSSジェネレーター
マウスの動きに合わせて傾き、表面に光が反射したような効果を持つ透過カードのデザインを生成します。
プレビュー
<div class="card-perspective">
<div class="glass-card">
<div class="reflection"></div>
<div class="content">
<span class="card-title">3D Glass Card</span>
<p>Mouse over me to see the 3D tilt and reflection effect.</p>
<button>Learn More</button>
</div>
</div>
</div>
.card-perspective {
perspective: 1000px;
width: 320px;
height: 400px;
}
.glass-card {
width: 100%;
height: 100%;
background: {{card_color}}3a; /* 透明度あり */
backdrop-filter: blur({{glass_blur}}px);
-webkit-backdrop-filter: blur({{glass_blur}}px);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 24px;
position: relative;
transition: transform 0.5s ease-out, box-shadow 0.1s ease;
transform-style: preserve-3d;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 25px 50px rgba(0, 0, 0, {{shadow_opacity}});
overflow: hidden;
}
.reflection {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(
135deg,
rgba(255, 255, 255, 0.4) 0%,
rgba(255, 255, 255, 0) 60%
);
pointer-events: none;
opacity: 0;
transition: opacity 0.3s ease;
}
.content {
padding: 40px;
text-align: center;
transform: translateZ(50px); /* 少し浮き上がらせる */
}
.content .card-title {
display: block;
color: {{text_color}};
margin-bottom: 15px;
font-size: 24px;
font-weight: bold;
}
.content p {
color: {{text_color}};
opacity: 0.8;
font-size: 14px;
line-height: 1.6;
}
.content button {
margin-top: 30px;
background: {{text_color}};
color: #000;
border: none;
padding: 10px 25px;
border-radius: 50px;
font-weight: bold;
cursor: pointer;
}
const card = document.querySelector('.glass-card');
const reflection = document.querySelector('.reflection');
const maxTilt = {{tilt_max}};
card.addEventListener('mousemove', (e) => {
const rect = card.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const centerX = rect.width / 2;
const centerY = rect.height / 2;
const rotateX = ((y - centerY) / centerY) * -maxTilt;
const rotateY = ((x - centerX) / centerX) * maxTilt;
// 移動中はレスポンスを良くするため transition を短くする
card.style.transition = 'transform 0.1s ease-out';
card.style.transform = `rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
// 反射光の制御
reflection.style.opacity = '1';
reflection.style.background = `radial-gradient(
circle at ${x}px ${y}px,
rgba(255, 255, 255, 0.3) 0%,
rgba(255, 255, 255, 0) 70%
)`;
});
card.addEventListener('mouseleave', () => {
// 離れた時はスムーズに戻るよう transition を長くする
card.style.transition = 'transform 0.6s cubic-bezier(0.23, 1, 0.32, 1)';
card.style.transform = 'rotateX(0deg) rotateY(0deg)';
reflection.style.opacity = '0';
});
機能・使い方
- マウスの動きに合わせて傾き、表面に光の反射が走るカードデザインを生成します。
- 透過・ボカシ効果:
backdrop-filterを使用して、背景をすりガラスのように透過させます。 - 3D アニメーション:
perspective設定により、奥行きのある動きを実現しています。 - マウス移動に合わせて反射光(グラデーション)の位置を動的に計算することで、質感を表現しています。
ポイント
JavaScriptではgetBoundingClientRect()を使用して、カード内でのマウス相対座標を計算しています。これにより、カードがどこに配置されていても正確な傾きを得られます。
マウスが離れた瞬間にtransition時間を切り替えることで、「追従時はキビキビと、戻る時はゆっくりスムーズに」という直感的な操作感を実現しています。