今天我們來創建一個炫酷的流光按鈕效果,它會隨着鼠標移動產生動態的流光和陰影效果。這個效果結合了CSS漸變、動畫和偽元素技術。
實現思路
- 使用
linear-gradient創建漸變背景 - 通過
::before偽元素實現流光效果 - 使用CSS動畫實現流光移動
- 添加懸停和點擊交互效果
- 結合JavaScript實現鼠標跟隨效果
下面是完整的實現代碼:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>炫酷的流光按鈕 - 每天一個CSS小樣式</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Arial, sans-serif;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #0f0f1e;
color: #fff;
padding: 20px;
overflow-x: hidden;
}
.header {
text-align: center;
margin-bottom: 40px;
max-width: 800px;
}
h1 {
font-size: 2.8rem;
background: linear-gradient(90deg, #ff0080, #00ffcc, #ff0080);
background-size: 200% auto;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
animation: textShine 4s linear infinite;
margin-bottom: 15px;
}
.subtitle {
font-size: 1.2rem;
color: #a0a0c0;
margin-bottom: 25px;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 30px;
margin-bottom: 40px;
max-width: 1000px;
}
.demo-card {
background: rgba(20, 20, 40, 0.7);
border-radius: 15px;
padding: 25px;
width: 300px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
border: 1px solid rgba(100, 100, 200, 0.1);
transition: transform 0.3s ease;
}
.demo-card:hover {
transform: translateY(-5px);
}
.demo-title {
font-size: 1.5rem;
margin-bottom: 20px;
color: #7aefff;
display: flex;
align-items: center;
gap: 10px;
}
.demo-title i {
font-size: 1.3rem;
}
.code-block {
background: #1a1a2e;
border-radius: 10px;
padding: 20px;
margin-top: 30px;
overflow-x: auto;
border-left: 4px solid #7aefff;
}
.code-block h3 {
color: #7aefff;
margin-bottom: 15px;
display: flex;
align-items: center;
gap: 10px;
}
.code-block pre {
color: #c5c8d3;
font-size: 0.95rem;
line-height: 1.5;
}
/* 按鈕1 - 基礎流光效果 */
.btn-1 {
position: relative;
display: inline-block;
padding: 18px 36px;
background: linear-gradient(90deg, #ff0080, #00ffcc, #ff0080);
background-size: 300% 100%;
border: none;
border-radius: 50px;
color: white;
font-size: 1.2rem;
font-weight: 600;
letter-spacing: 1px;
cursor: pointer;
overflow: hidden;
transition: all 0.4s ease;
box-shadow: 0 5px 20px rgba(255, 0, 128, 0.3);
z-index: 1;
}
.btn-1:hover {
animation: gradientMove 1.5s linear infinite;
box-shadow: 0 8px 30px rgba(0, 255, 204, 0.4);
transform: translateY(-3px);
}
.btn-1:active {
transform: translateY(0);
box-shadow: 0 3px 15px rgba(255, 0, 128, 0.3);
}
.btn-1::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
transform: translateX(-100%);
transition: transform 0.6s ease;
z-index: -1;
}
.btn-1:hover::before {
transform: translateX(100%);
}
/* 按鈕2 - 鼠標跟隨流光效果 */
.btn-2 {
position: relative;
display: inline-block;
padding: 18px 36px;
background: #0f0f1e;
border: 2px solid transparent;
border-radius: 10px;
color: white;
font-size: 1.2rem;
font-weight: 600;
letter-spacing: 1px;
cursor: pointer;
overflow: hidden;
transition: all 0.4s ease;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.btn-2::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(90deg, #ff0080, #00ffcc, #ff0080);
background-size: 300% 100%;
z-index: -2;
opacity: 0;
transition: opacity 0.4s ease;
}
.btn-2:hover::before {
opacity: 1;
animation: gradientMove 2s linear infinite;
}
.btn-2::after {
content: '';
position: absolute;
top: -2px;
left: -2px;
right: -2px;
bottom: -2px;
background: linear-gradient(90deg, #ff0080, #00ffcc, #ff0080);
background-size: 300% 100%;
border-radius: 12px;
z-index: -1;
opacity: 0;
transition: opacity 0.4s ease;
}
.btn-2:hover::after {
opacity: 1;
animation: gradientMove 2s linear infinite;
}
/* 按鈕3 - 3D流光效果 */
.btn-3 {
position: relative;
display: inline-block;
padding: 20px 40px;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
border: none;
border-radius: 12px;
color: white;
font-size: 1.3rem;
font-weight: 700;
letter-spacing: 1px;
cursor: pointer;
overflow: hidden;
transition: all 0.4s ease;
box-shadow:
0 10px 20px rgba(106, 17, 203, 0.3),
0 6px 6px rgba(0, 0, 0, 0.2),
inset 0 1px 0 rgba(255, 255, 255, 0.2);
transform-style: preserve-3d;
perspective: 500px;
}
.btn-3:hover {
transform: translateY(-5px) rotateX(10deg);
box-shadow:
0 15px 30px rgba(106, 17, 203, 0.4),
0 10px 10px rgba(0, 0, 0, 0.2),
inset 0 1px 0 rgba(255, 255, 255, 0.2);
}
.btn-3:active {
transform: translateY(0) rotateX(0);
}
.btn-3 .shine {
position: absolute;
top: 0;
left: -100%;
width: 50%;
height: 100%;
background: linear-gradient(to right, transparent, rgba(255, 255, 255, 0.3), transparent);
transform: skewX(-20deg);
transition: left 0.8s ease;
}
.btn-3:hover .shine {
left: 150%;
}
/* 按鈕4 - 霓虹流光效果 */
.btn-4 {
position: relative;
display: inline-block;
padding: 18px 36px;
background: transparent;
border: 2px solid #00ffcc;
border-radius: 8px;
color: #00ffcc;
font-size: 1.2rem;
font-weight: 600;
letter-spacing: 2px;
cursor: pointer;
overflow: hidden;
transition: all 0.5s ease;
text-shadow: 0 0 5px #00ffcc;
box-shadow:
0 0 10px rgba(0, 255, 204, 0.3),
inset 0 0 10px rgba(0, 255, 204, 0.1);
z-index: 1;
}
.btn-4::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(0, 255, 204, 0.2), transparent);
transform: translateX(-100%);
transition: transform 0.6s ease;
z-index: -1;
}
.btn-4:hover {
color: #0f0f1e;
background: #00ffcc;
border-color: #00ffcc;
text-shadow: none;
box-shadow:
0 0 20px rgba(0, 255, 204, 0.6),
0 0 40px rgba(0, 255, 204, 0.4),
inset 0 0 10px rgba(255, 255, 255, 0.3);
}
.btn-4:hover::before {
transform: translateX(100%);
}
/* 動畫定義 */
@keyframes gradientMove {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
@keyframes textShine {
0% {
background-position: 0% 50%;
}
100% {
background-position: 200% 50%;
}
}
.footer {
margin-top: 30px;
text-align: center;
color: #707090;
font-size: 0.9rem;
}
.instructions {
background: rgba(20, 20, 40, 0.7);
border-radius: 15px;
padding: 20px;
margin-bottom: 30px;
max-width: 800px;
border-left: 5px solid #ff0080;
}
.instructions h3 {
color: #ff8ad0;
margin-bottom: 10px;
display: flex;
align-items: center;
gap: 10px;
}
.instructions ul {
padding-left: 20px;
}
.instructions li {
margin-bottom: 8px;
line-height: 1.5;
}
/* 響應式設計 */
@media (max-width: 768px) {
.container {
flex-direction: column;
align-items: center;
}
.demo-card {
width: 100%;
max-width: 400px;
}
h1 {
font-size: 2.2rem;
}
}
</style>
</head>
<body>
<div class="header">
<h1><i class="fas fa-magic"></i> 每天一個CSS小樣式</h1>
<p class="subtitle">今日主題:炫酷的流光按鈕效果</p>
</div>
<div class="instructions">
<h3><i class="fas fa-lightbulb"></i> 設計思路</h3>
<ul>
<li>使用<code>linear-gradient</code>創建漸變背景,並通過動畫改變背景位置實現流光效果</li>
<li>利用<code>::before</code>和<code>::after</code>偽元素添加額外的視覺效果</li>
<li>結合<code>transform</code>屬性實現3D懸浮效果</li>
<li>使用<code>box-shadow</code>增強按鈕的立體感和發光效果</li>
<li>通過<code>transition</code>和<code>animation</code>創建平滑的交互效果</li>
</ul>
</div>
<div class="container">
<div class="demo-card">
<h3 class="demo-title"><i class="fas fa-play"></i> 基礎流光按鈕</h3>
<p>具有動態漸變背景和懸停流光效果</p>
<div style="text-align: center; margin-top: 25px;">
<button class="btn-1">流光按鈕</button>
</div>
</div>
<div class="demo-card">
<h3 class="demo-title"><i class="fas fa-mouse-pointer"></i> 鼠標跟隨流光</h3>
<p>懸停時顯示漸變邊框和背景流光</p>
<div style="text-align: center; margin-top: 25px;">
<button class="btn-2">鼠標跟隨</button>
</div>
</div>
<div class="demo-card">
<h3 class="demo-title"><i class="fas fa-cube"></i> 3D流光按鈕</h3>
<p>具有3D旋轉效果和動態光澤</p>
<div style="text-align: center; margin-top: 25px;">
<button class="btn-3">
<span class="shine"></span>
3D流光
</button>
</div>
</div>
<div class="demo-card">
<h3 class="demo-title"><i class="fas fa-lightbulb"></i> 霓虹流光按鈕</h3>
<p>霓虹燈風格,懸停時變色發光</p>
<div style="text-align: center; margin-top: 25px;">
<button class="btn-4">霓虹流光</button>
</div>
</div>
</div>
<div class="code-block">
<h3><i class="fas fa-code"></i> 核心CSS代碼示例(基礎流光按鈕)</h3>
<pre><code>
/* 基礎流光按鈕 */
.btn-1 {
position: relative;
padding: 18px 36px;
background: linear-gradient(90deg, #ff0080, #00ffcc, #ff0080);
background-size: 300% 100%; /* 背景放大3倍以便移動 */
border: none;
border-radius: 50px;
color: white;
font-size: 1.2rem;
font-weight: 600;
cursor: pointer;
overflow: hidden;
transition: all 0.4s ease;
box-shadow: 0 5px 20px rgba(255, 0, 128, 0.3);
}
/* 懸停時背景動畫 */
.btn-1:hover {
animation: gradientMove 1.5s linear infinite;
box-shadow: 0 8px 30px rgba(0, 255, 204, 0.4);
transform: translateY(-3px);
}
/* 流光動畫 */
@keyframes gradientMove {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
/* 添加白色流光覆蓋層 */
.btn-1::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(90deg,
transparent,
rgba(255, 255, 255, 0.2),
transparent);
transform: translateX(-100%);
transition: transform 0.6s ease;
}
.btn-1:hover::before {
transform: translateX(100%);
}
</code></pre>
</div>
<div class="footer">
<p>每天一個CSS小樣式 | 炫酷的流光按鈕效果</p>
<p>使用純CSS實現,無需JavaScript(部分交互效果除外)</p>
</div>
<script>
// 為按鈕2添加簡單的鼠標跟隨效果
document.querySelector('.btn-2').addEventListener('mousemove', function(e) {
const rect = this.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// 根據鼠標位置調整漸變中心
this.style.setProperty('--mouse-x', `${x}px`);
this.style.setProperty('--mouse-y', `${y}px`);
});
// 為所有按鈕添加點擊漣漪效果
document.querySelectorAll('.btn-1, .btn-2, .btn-3, .btn-4').forEach(button => {
button.addEventListener('click', function(e) {
// 創建漣漪元素
const ripple = document.createElement('span');
const rect = this.getBoundingClientRect();
const size = Math.max(rect.width, rect.height);
const x = e.clientX - rect.left - size / 2;
const y = e.clientY - rect.top - size / 2;
ripple.style.cssText = `
position: absolute;
border-radius: 50%;
background: rgba(255, 255, 255, 0.6);
transform: scale(0);
animation: ripple-animation 0.6s linear;
width: ${size}px;
height: ${size}px;
top: ${y}px;
left: ${x}px;
pointer-events: none;
`;
this.appendChild(ripple);
// 移除漣漪元素
setTimeout(() => {
ripple.remove();
}, 600);
});
});
// 添加漣漪動畫到頁面
const style = document.createElement('style');
style.textContent = `
@keyframes ripple-animation {
to {
transform: scale(4);
opacity: 0;
}
}
`;
document.head.appendChild(style);
</script>
</body>
</html>
效果説明
這個頁面展示了四種不同風格的流光按鈕:
- 基礎流光按鈕:具有動態漸變背景,懸停時背景動畫和白色流光劃過效果
- 鼠標跟隨流光:懸停時顯示漸變邊框和背景,具有流光效果
- 3D流光按鈕:具有3D旋轉效果,懸停時按鈕傾斜並有光澤劃過
- 霓虹流光按鈕:霓虹燈風格,懸停時變色發光,並有流光劃過
技術要點
- 使用
linear-gradient創建多彩漸變背景 - 通過
background-size放大背景,再通過動畫移動背景位置實現流光效果 - 利用
::before和::after偽元素添加額外的視覺效果 - 使用
transform屬性實現3D效果和動畫 - 結合
box-shadow創建發光和立體效果 - 使用CSS動畫和過渡實現平滑的交互效果
每個按鈕都是純CSS實現,只有簡單的JavaScript用於增強交互體驗(如點擊漣漪效果)。