我有一个ul列表,并且我已经给出了悬停效果,使用:after它可以在悬停上正常工作,但是现在我希望它将从中心开始而不是从左开始。
我的代码:
ul.my-list{ margin: 20px; padding: 0px; width: 200px;}
ul.my-list li{ list-style: none; position: relative; display: inline;}
ul.my-list li a{ color:#333; text-decoration: none;}
ul.my-list li:after{ content: ''; position: absolute; left: 0px; bottom: -2px; width:0px; height: 2px; background: #333; transition: all 0.45s;}
ul.my-list li:hover:after{ width: 100%;}
ul.my-list li a:hover{ text-decoration: none;}Run Code Online (Sandbox Code Playgroud)
<ul class="my-list">
<li><a href="#">Welcome to my website</a></li>
</ul>Run Code Online (Sandbox Code Playgroud)
您可以使用如下背景简化代码
ul.my-list {
margin: 20px;
padding: 0px;
width: 200px;
}
ul.my-list li {
list-style: none;
display: inline-block;
padding-bottom:2px; /*the space for the gradient*/
background: linear-gradient(#333,#333) center bottom; /*OR bottom right OR bottom left*/
background-size: 0% 2px; /*width:0% height:2px*/
background-repeat:no-repeat; /* Don't repeat !!*/
transition: all 0.45s;
}
ul.my-list li a {
color: #333;
text-decoration: none;
}
ul.my-list li:hover {
background-size: 100% 2px; /*width:100% height:2px*/
}Run Code Online (Sandbox Code Playgroud)
<ul class="my-list">
<li><a href="#">Welcome to my website</a></li>
</ul>Run Code Online (Sandbox Code Playgroud)
快速解决方案:
将原始位置移至left:50%,然后在悬停时将其移至left:0。
ul.my-list {
margin: 20px;
padding: 0px;
width: 200px;
}
ul.my-list li {
list-style: none;
position: relative;
display: inline;
}
ul.my-list li a {
color: #333;
text-decoration: none;
}
ul.my-list li:after {
content: '';
position: absolute;
left: 50%;
bottom: -2px;
width: 0px;
height: 2px;
background: #333;
transition: all 0.45s;
}
ul.my-list li:hover:after {
width: 100%;
left: 0;
}
ul.my-list li a:hover {
text-decoration: none;
}Run Code Online (Sandbox Code Playgroud)
<ul class="my-list">
<li><a href="#">Welcome to my website</a></li>
</ul>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
157 次 |
| 最近记录: |