簡介
這一次,我們正在創造一個有用的設置與對CSS3的多重背景和動畫的力量動畫按鈕。通過此按鈕包,您可以很容易地變成一個動畫按鈕,在您的網頁上的任何鏈接只是指定一個類名。沒有必要JavaScript。四色主題和三個大小也可通過分配額外的類名。
HTML
要打開網頁上的常規鏈接成一個奇特的動畫CSS3的按鈕,你只需要到指定的按鈕類和支持的顏色之一。請參閱下面的一些例子:
1
2
3
|
<
a
href
=
"#"
class
=
"button blue big"
>Download</
a
>
<
a
href
=
"#"
class
=
"button blue medium"
>Submit</
a
>
<
a
href
=
"#"
class
=
"button small blue rounded"
>Submit</
a
>
|
有四種顏色類 – 藍色,綠色,橙色和灰色。其余的類,你看到分配給上面的鏈接,是可選的。您可以選擇從規模小,中,大,一類 – 圓潤,它創建了一個按鈕更加圓潤的版本。
類名選擇,所以他們簡單易記,但這就提出了一個與頁面上的一些類的沖突的可能性。
現在讓我們在仔細一看,做到這一點的CSS類。
CSS
所有動畫按鈕的CSS代碼駐留在buttons.css。這使得很容易下降到現有的項目,並使用它。
請注意,整個的下面的代碼,我定義了兩個版本在一些地方的同一財產。這與瀏覽器處理CSS定義的方式。他們逐一解析規則,並將其應用,忽略了他們不明白的。這樣我們就可以有一個理解所有的規則,這是普通版,一個CSS3的啟用,將舊的忽視。
buttons/buttons.css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
.button{
font
:
15px
Calibri,
Arial
,
sans-serif
;
/* A semi-transparent text shadow */
text-shadow
:
1px
1px
0
rgba(
255
,
255
,
255
,
0.4
);
/* Overriding the default underline styling of the links */
text-decoration
:
none
!important
;
white-space
:
nowrap
;
display
:inline-
block
;
vertical-align
:
baseline
;
position
:
relative
;
cursor
:
pointer
;
padding
:
10px
20px
;
background-repeat
:
no-repeat
;
/* The following two rules are fallbacks, in case
the browser does not support multiple backgrounds. */
background-position
:
bottom
left
;
background-image
:
url
(
'button_bg.png'
);
/* CSS3 background positioning property with multiple values. The background
images themselves are defined in the individual color classes */
background-position
:
bottom
left
,
top
right
,
0
0
,
0
0
;
background-
clip
:border-box;
/* Applying a default border radius of 8px */
-moz-border-radius:
8px
;
-webkit-border-radius:
8px
;
border-radius:
8px
;
/* A 1px highlight inside of the button */
-moz-box-shadow:
0
0
1px
#fff
inset
;
-webkit-box-shadow:
0
0
1px
#fff
inset
;
box-shadow:
0
0
1px
#fff
inset
;
/* Animating the background positions with CSS3 */
/* Currently works only in Safari/Chrome */
-webkit-transition:background-position
1
s;
-moz-transition:background-position
1
s;
-o-transition:background-position
1
s;
transition:background-position
1
s;
}
.button:hover{
/* The first rule is a fallback, in case the browser
does not support multiple backgrounds
*/
background-position
:
top
left
;
background-position
:
top
left
,
bottom
right
,
0
0
,
0
0
;
}
|
我們需要做的第一件事是定義按鈕類。這是按鈕的骨干,因為它適用於定位,字體和背景樣式。
首先是與字體相關的樣式,在這之后如下顯示屬性。它被設置為inline – block的,這使得它能夠坐在其周圍的文本(如內聯元素)的同一行,但也像一個方面的填充和利潤率塊。
在某一時刻,你會看到每個按鈕有四個背景圖像應用到它。雖然這聽起來很嚇人,只有一個文件,實際上是要求從服務器。前兩個背景,左下角和右上角部分泡沫圖像,你可以看到下面的插圖,和其他兩個是純CSS的梯度。
正如我上面提到的的,泡沫的背景是顯示為兩個單獨的圖像,背景位置屬性的偏移。使用CSS3的過渡屬性,我們定義動畫,在這兩個版本的背景圖片幻燈片的頂部或底部恭恭敬敬,你看到懸停按鈕時產生泡沫效應。
現在讓我們說幾句話的大小和圓潤類。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/* The three buttons sizes */
.button.big {
font-size
:
30px
;}
.button.
medium
{
font-size
:
18px
;}
.button.
small
{
font-size
:
13px
;}
/* A more rounded button */
.button.rounded{
-moz-border-radius:
4em
;
-webkit-border-radius:
4em
;
border-radius:
4em
;
}
|
這里有三個大小類 – 小型,中型和大型,圓角類。根據自己的文字大小的按鈕規模。這種方式沒有明確的寬度和高度指定。
現在讓我們有趣的部分 – 顏色。只有藍色按鈕的定義如下,其余幾乎是相同的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/* BlueButton */
.
blue
.button{
color
:
#0f4b6d
!important
;
border
:
1px
solid
#84acc3
!important
;
/* A fallback background color */
background-color
:
#48b5f2
;
/* Specifying a version with gradients according to */
background-image
:
url
(
'button_bg.png'
),
url
(
'button_bg.png'
),
-moz-radial-gradient(
center
bottom
,
circle
,
rgba(
89
,
208
,
244
,
1
)
0
,rgba(
89
,
208
,
244
,
0
)
100px
),
-moz-linear-gradient(
#4fbbf7
,
#3faeeb
);
background-image
:
url
(
'button_bg.png'
),
url
(
'button_bg.png'
),
-webkit-gradient( radial,
50%
100%
,
0
,
50%
100%
,
100
,
from(rgba(
89
,
208
,
244
,
1
)), to(rgba(
89
,
208
,
244
,
0
))),
-webkit-gradient(linear,
0%
0%
,
0%
100%
, from(
#4fbbf7
), to(
#3faeeb
));
}
.
blue
.button:hover{
background-color
:
#63c7fe
;
background-image
:
url
(
'button_bg.png'
),
url
(
'button_bg.png'
),
-moz-radial-gradient(
center
bottom
,
circle
,
rgba(
109
,
217
,
250
,
1
)
0
,rgba(
109
,
217
,
250
,
0
)
100px
),
-moz-linear-gradient(
#63c7fe
,
#58bef7
);
background-image
:
url
(
'button_bg.png'
),
url
(
'button_bg.png'
),
-webkit-gradient( radial,
50%
100%
,
0
,
50%
100%
,
100
,
from(rgba(
109
,
217
,
250
,
1
)), to(rgba(
109
,
217
,
250
,
0
))),
-webkit-gradient(linear,
0%
0%
,
0%
100%
, from(
#63c7fe
), to(
#58bef7
));
}
|
每種顏色的類定義了獨特的一套獨特的屬性 – 按鈕的文本標簽的顏色,文字陰影和背景圖像。注意,我們使用的背景屬性按鈕添加多個圖像。他們是分層的頂部,首先出現在上面定義的。
只有Mozilla和Webkit瀏覽器目前支持CSS的梯度,但與完全不同的語法。其余的瀏覽器將顯示回退的背景顏色。您可能已經注意到,我們沒有包括一個免費版本的漸變規則的前綴。這是由於梯度不是一個CSS規范尚未正式的一部分的事實,並沒有首選語法協議。
在上面的片段中,你可以看到,我們定義在它上面的線性漸變和徑向之一。為了使漸變交融,更順利的WebKit和Mozilla的語法,我們定義,這使得外完全透明的漸變顏色的RGBA徑向之一。
有了這個,我們的CSS3動畫泡沫按鈕完成!
總結
這些按鈕是完全基於CSS和整合是非常簡單 – 只是下降的按鈕文件夾在您的項目中的某處。通過修改CSS文件,您可以創建自己的顏色和形狀。