用一點 canvas 與 JavaScript,就能讓按鈕、標題甚至圖片散發出閃爍的星光粒子。這個範例把 sparkle 效果封裝成一個函式,只要指定目標元素就能套用,非常適合活動頁、節慶頁或需要一點驚喜感的互動區塊。
體驗效果:https:///keleyi/phtml/html5/30.htm(請使用支援 HTML5 的瀏覽器開啟)
頁面主體很簡單:一個標題、兩個 sparkley 按鈕、一段說明文字,以及一張要套特效的圖片。核心是把 class="sparkley" 加在目標元素上,再由 JavaScript 去掃描並處理。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5 夢幻特效 —— 為任意元素添加魔幻效果</title>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>
A little canvas script to <span>add magic</span> to DOM elements!</h1>
<button class="sparkley">
Everything's better with sparkles!</button>
<button class="sparkley last">
I eat rainbows and poop butterflies!!</button>
<div style="text-align: center; clear: both; margin-top: 30px">
<span id="keleyi"><a href="https://">首頁</a> <a href="https:///keleyi/phtml/">
特效庫</a> <a href="https:///a/bjae/7othu7n3.htm">原文</a> 請使用<a href="https:///a/bjac/g039tue3.htm">支援 HTML5 的瀏覽器</a>查看本頁。
</span>
</div>
<div class="img">
<img id="image" src="https:///keleyi/phtml/html5/30/Rain-l.jpg">
</div>
<p>
It won't work on images or DOM elements which cannot contain other elements, unless
we wrap them.</p>
<script src='https:///keleyi/pmedia/jquery/jquery-1.11.2.min.js'></script>
<script src='https:///keleyi/phtml/html5/30/js/rbeix.js'></script>
<script src="https:///keleyi/phtml/html5/30/js/index.js"></script>
</body>
</html>
範例的 CSS 分兩部分:開頭引入了 normalize.css 作為樣式重置(處理 HTML5 元素在各瀏覽器下的 display 差異、表單元素的字型繼承、box-sizing 等),後半段則是這個 demo 自己的視覺設計。以下擷取與特效呈現最相關的部分:
a
{
color: White;
}
.sparkley
{
background: #3e5771;
color: white;
border: none;
padding: 16px 36px;
font-weight: normal;
border-radius: 3px;
transition: all 0.25s ease;
box-shadow: 0 38px 32px -23px black;
margin: 0 1em 1em;
}
.sparkley:hover
{
background: #2c3e50;
color: rgba(255, 255, 255, 0.2);
}
html
{
font-family: Lato;
font-weight: 200;
font-size: 1em;
text-align: center;
color: #ddd;
min-height: 100%;
background: #092756;
background: -moz-radial-gradient(0% 100%, ellipse cover, rgba(35, 24, 82, 0.22) 10%, rgba(138, 114, 76, 0) 40%), -moz-linear-gradient(top, rgba(57, 173, 219, 0.25) 0%, rgba(42, 60, 87, 0.4) 100%), -moz-linear-gradient(-45deg, #670d10 0%, #092756 100%);
background: -webkit-radial-gradient(0% 100%, ellipse cover, rgba(35, 24, 82, 0.22) 10%, rgba(138, 114, 76, 0) 40%), -webkit-linear-gradient(top, rgba(57, 173, 219, 0.25) 0%, rgba(42, 60, 87, 0.4) 100%), -webkit-linear-gradient(-45deg, #670d10 0%, #092756 100%);
background: -webkit-radial-gradient(0% 100%, ellipse cover, rgba(35, 24, 82, 0.22) 10%, rgba(138, 114, 76, 0) 40%), linear-gradient(to bottom, rgba(57, 173, 219, 0.25) 0%, rgba(42, 60, 87, 0.4) 100%), linear-gradient(135deg, #670d10 0%, #092756 100%);
}
html body
{
padding: 50px;
}
h1 span, h2 span, h3 span
{
font-family: Montez;
font-size: 1.9em;
font-weight: 300;
margin: 0 0.3em;
color: #ff0080;
}
h1
{
font-size: 1.9em;
width: 900px;
margin: 0 auto 1em;
text-shadow: 0 2px 1px black;
}
.img
{
margin: 40px auto 0;
width: 500px;
display: block;
}
.img img
{
width: 100%;
border-radius: 5px;
}
p
{
padding: 5px 10px;
display: inline-block;
margin: 10px auto;
}
背景用了多層疊加的 radial-gradient 與 linear-gradient,做出深藍夜空加一抹暗紅暈染的效果,正好襯托星光。按鈕則靠 transition: all 0.25s ease 讓 hover 時的顏色變化變得柔和。
three 個 script 依序是 jQuery、星光特效本體(rbeix.js)與初始化腳本(index.js)。套用時只要給目標元素加上 sparkley 類別,特效腳本會在元素上層覆蓋一層 canvas 來繪製閃爍粒子。
如頁面下方那段英文提示所說:這個效果無法直接套用在 img 這類不能包含其他子元素的標籤上,因為特效需要往目標元素裡插入 canvas 節點。解法很簡單——外面多包一層 div,把 sparkley 加在外層容器上即可。
另外提醒,這類持續重繪的 canvas 動畫會佔用不少 CPU,建議只在真正需要的區塊啟用,並考慮配合 Intersection Observer 在元素離開視窗時暫停繪製。