之前的文章《新手篇:如何用ccs制作一個簡單的布局(附代碼)》中,給大家介紹了如何用ccs制作一個簡單的布局。下面本篇文章給大家介紹怎么使用css3制作按鈕添加動態(tài)效果,我們一起看看怎么做。
css
如何實現button
按鈕效果?
HTML結構:
首先定義一個body
,使用button
按鈕,添加文字value
設置為“開始游戲”以方便設置class
轉為id
選擇器。
<body> <input id="search" name="cx" type="button" value="開始游戲" class="btn search"> </body>
效果代碼
效果出來了,能看到按鈕效果了,但是沒有給它添加動態(tài)裝飾,通過使用css
給它添加動態(tài)效果,一起看看怎么做。
css編輯代碼:
1、在style
之間,對search
進行樣式初始化,添加設置高度和寬度,然后使用設置背景background
,設置no-repeat
這個屬性背景圖將不會被重復。
.search { width: 185px; height: 70px; background: url(images/btn_08.jpg) no-repeat center; }
代碼效果
2、接著,給css3
按鈕添加圓角效果設置屬性每個border
的四個值,最后設置居中對齊使用float: left
。
border-radius: 8px; -webkit-border-radius: 8px; -o-border-radius: 8px; -moz-border-radius: 8px; float: left;
代碼效果
四點邊圓角效果出來了
3、再給search
進行樣式添加字體大小、文本對齊方式、字體的粗細,設置border
元素所有邊框的樣式、顏色、形狀。
font-size: 30px; text-align: center; font-weight: bold; border: none; color: #fff; cursor: pointer; line-height: 70px; font-family: 微軟雅黑;
4、在style
之間,對btn
進行樣式初始化,添加設置高度和寬度,然后使用設置背景background
。
.btn { width: 383px; height: 70px;line-height: 0; border: 2px solid #a2f3ff; background: #f3682d; }
代碼效果
5、再給btn
進行樣式添加字體大小、文本對齊方式、字體的粗細,設置border
元素所有邊框的樣式、顏色、形狀。
border-radius: 37px; -webkit-border-radius: 37px; -o-border-radius: 37px; -moz-border-radius: 37px; text-shadow: 3px 2px #d4481b; -webkit-text-shadow: 3px 2px #d4481b; -o-text-shadow: 3px 2px #d4481b; -moz-text-shadow: 3px 2px #d4481b; font-family: 微軟雅黑;
代碼效果
6、將動畫與search
綁定
#search{ animation: breathe 1.1s infinite;
7、使用@keyframes
規(guī)則,創(chuàng)建動畫。
@keyframes breathe{ 0%{ transform: scale(.99); } 50%{ transform: scale(1.03); } 100%{ transform: scale(.99); } }
代碼效果
ok,編輯代碼完成。
完整代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>button按鈕</title> <style type="text/css"> .search { width: 185px; height: 70px; background: url(images/btn_08.jpg) no-repeat center; border-radius: 8px; -webkit-border-radius: 8px; -o-border-radius: 8px; -moz-border-radius: 8px; float: left; font-size: 30px; text-align: center; font-weight: bold; border: none; color: #fff; cursor: pointer; line-height: 70px; font-family: 微軟雅黑; } .btn { width: 383px; height: 70px;line-height: 0; border: 2px solid #a2f3ff; background: #f3682d; margin: 22px 0 0 17px; border-radius: 37px; -webkit-border-radius: 37px; -o-border-radius: 37px; -moz-border-radius: 37px; text-shadow: 3px 2px #d4481b; -webkit-text-shadow: 3px 2px #d4481b; -o-text-shadow: 3px 2px #d4481b; -moz-text-shadow: 3px 2px #d4481b; font-family: 微軟雅黑; } #search{ animation: breathe 1.1s infinite; } @keyframes breathe{ 0%{ transform: scale(.99); } 50%{ transform: scale(1.03); } 100%{ transform: scale(.99); } } </style> </head> <body> <input id="search" name="cx" type="button" value="開始游戲" class="btn search"> </body> </html>
推薦學習:CSS3視頻教程