如果我从float:left
删除.action-button
属性,则转换动画不起作用。你能解释一下这里发生了什么吗?
标记:
<a href="#" class="action-button animate green">Are</a>
样式:
.action-button
{
position: relative;
padding: 10px 40px;
margin: 0px 10px 10px 0px;
float: left;
border-radius: 8px;
font-family: 'Pacifico', cursive;
font-size: 25px;
color: #FFF;
text-decoration: none;
}
.green
{
background-color: #82BF56;
border-bottom: 5px solid #669644;
text-shadow: 0px -2px #669644;
}
.action-button:active
{
transform: translate(0px,5px);
-webkit-transform: translate(0px,5px);
border-bottom: 1px solid;
}
Codepen第17行:http://codepen.io/koriolis/pen/euAEg
答案 0 :(得分:1)
这是因为a
元素是inline
元素,因为它们对齐父元素并且不会清除任何一方。
当您添加float: left
时,它会将元素从inline
更改为block
,但也会移除clear: both
并允许元素彼此相邻对齐。
如果您不希望标记在页面上浮动,则可以使用float
获得与inline-block
相同的效果。
.action-button {
position: relative;
padding: 10px 40px;
margin: 0px 10px 10px 0px;
display: inline-block;
border-radius: 8px;
font-family: 'Pacifico', cursive;
font-size: 25px;
color: #FFF;
text-decoration: none;
}
答案 1 :(得分:0)
a
- 默认display: inline
float: left
- 显示属性设置为阻止float: left
- a
display: inline
时,
使用display: inline-block
代替float: left
body {
padding: 50px;
}
.animate {
transition: all 0.1s;
-webkit-transition: all 0.1s;
}
.action-button {
position: relative;
padding: 10px 40px;
margin: 0px 10px 10px 0px;
display: inline-block;
border-radius: 8px;
font-family:'Pacifico', cursive;
font-size: 25px;
color: #FFF;
text-decoration: none;
}
.blue {
background-color: #3498DB;
border-bottom: 5px solid #2980B9;
text-shadow: 0px -2px #2980B9;
}
.red {
background-color: #E74C3C;
border-bottom: 5px solid #BD3E31;
text-shadow: 0px -2px #BD3E31;
}
.green {
background-color: #82BF56;
border-bottom: 5px solid #669644;
text-shadow: 0px -2px #669644;
}
.yellow {
background-color: #F2CF66;
border-bottom: 5px solid #D1B358;
text-shadow: 0px -2px #D1B358;
}
.action-button:active {
transform: translate(0px, 5px);
-webkit-transform: translate(0px, 5px);
border-bottom: 1px solid;
}
&#13;
<a href="#" class="action-button animate blue">Hello</a>
<a href="#" class="action-button animate red">How</a>
<a href="#" class="action-button animate green">Are</a>
<a href="#" class="action-button animate yellow">You?</a>
&#13;