按钮模版:btn-8

效果展示

实现思路

这个效果的实现主要是利用了 CSS 优先级的规则

topbottom 同时存在时,top 的优先级高于 bottom

leftright 同时存在时,left 的优先级高于 right


鼠标没有悬浮时,width: 0; right 0;,相当于一个没有宽度的容器定为在父盒子的右边

当鼠标悬浮时,width: 100%;,容器和父盒子大小一样大了,正常情况下应该是从右向左填充,因为容器是在父盒子的右边

left: 0;left 的优先级大于 right,所以容器从左向右填充

实现代码

1
<button class="btn-8">BTN-8</button>
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
.btn-8 {
padding: 16px 28px;
border-radius: 8px;
background: linear-gradient(315deg, #89d8d3 0%, #03c8a8 74%);
border: none;
z-index: 1;
transition: all 0.3s ease;
color: #fff;
position: relative;
cursor: pointer;
}
.btn-8:after {
position: absolute;
content: "";
width: 0;
height: 100%;
top: 0;
right: 0;
z-index: -1;
background-color: linear-gradient(315deg, #4dccc6 0%, #96e4df 74%);
border-radius: 8px;
box-shadow:inset 2px 2px 2px 0px rgba(255,255,255,.5),
7px 7px 20px 0px rgba(0,0,0,.1),
4px 4px 5px 0px rgba(0,0,0,.1);
transition: all 0.3s ease;
}
.btn-8:hover:after {
left: 0;
width: 100%;
}