使用 CSS3 可以為按鈕加入圓角、陰影與動畫。以下範例以深色按鈕為基礎,在 hover 時觸發綠色脈衝效果。
.button, .button:visited {
background: #222 url(overlay.png) repeat-x;
display: inline-block;
padding: 5px 10px 6px;
color: #fff;
text-decoration: none;
border-radius: 6px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
text-shadow: 0 -1px 1px rgba(0, 0, 0, 0.25);
cursor: pointer;
}
.button:hover {
background-color: #111;
animation-duration: 2s;
animation-timing-function: ease-out;
animation-iteration-count: infinite;
}
@-webkit-keyframes greenPulse {
from { background-color: #91bd09; box-shadow: 0 0 9px #333; }
50% { background-color: #B4E02C; box-shadow: 0 0 18px #91bd09; }
to { background-color: #91bd09; box-shadow: 0 0 9px #333; }
}
.green.button:hover { -webkit-animation-name: greenPulse; }
HTML 結構可將按鈕套用在 a 或 input 元素上,並加上 button、green 等 class。不同瀏覽器對動畫支援程度不同,正式環境建議加上必要的瀏覽器前綴並測試。