简介
这一次,我们正在创造一个有用的设置与对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文件,您可以创建自己的颜色和形状。