[英]Link hover: animated bottom border/underline
I've already known this underline/border-bottom animation for a long time (fiddle: https://jsfiddle.net/0ou3o9rn/4/ ), but I can't seem to find out how I'd have to style my codes correctly... Nothing works. 我已经知道这个下划线/边框底部动画很长一段时间了(小提琴: https : //jsfiddle.net/0ou3o9rn/4/ ),但我似乎无法弄清楚我是如何设计风格的正确编码......没有任何作用。
CSS: CSS:
.lists li {
position: absolute;
display:block;
left: 0;
top:90%;
margin:0 auto;
height: 2px;
background-color: #000;
width: 0%;
transition: width 1s;}
HTML: HTML:
<div class="lists"><ul>
<li><a href="/">link 1</a></li>
<li><a href="/">link 2</a></li>
<li><a href="/">link 3</a></li>
</ul></div>
This for example doesn't work, but I still want an animated underline to appear when I hover over the li
s/ a
s. 例如,这不起作用,但当我将鼠标悬停在
li
s / a
s上时,我仍然希望显示动画下划线。 Can anyone help me? 谁能帮我? Thank you!
谢谢!
No need of extra markup (pseudo would do the job too anyway ), you could simply use a background-image (or gradient) and use background-size
to expand/shrink the effect.background-position can be sused to set where effect can start (demo below does : left, center, right) 不需要额外的标记(伪也无论如何都要做),您可以简单地使用背景图像(或渐变)并使用
background-size
来扩展/缩小效果。可以使用background-size
位置来设置效果可以开始(下面的演示:左,中,右)
.lists li { display: inline-block; margin:0 1em; } .lists li a { display: block; padding: 3px 1em 3px; text-decoration: none; background: linear-gradient(to left, gold, gold) no-repeat bottom center; background-size: 0% 3px; /* will not show */ transition: 0.25s; } .lists li a:hover { background-size: 100% 3px; /* will expand whole width */ } /* some more styling ? */ .lists { background:gray; } .lists li:first-of-type a { background-position: bottom left; } .lists li:last-of-type a { background-position: bottom right; } .lists li a { background-color: #222; color: #ace }
<div class="lists"> <ul> <li><a href="/">link 1</a> </li> <li><a href="/">link 2</a> </li> <li><a href="/">link 3</a> </li> </ul> </div>
Slider is a box with 2px height. 滑块是一个2px高度的盒子。 Initially widht of block is 0px.
最初的阻止是0px。 When user hovers over the #name, then width of slider becomes 100%.
当用户将鼠标悬停在#name上时,滑块的宽度变为100%。 Now css transition is applied on this width.
现在css过渡应用于此宽度。 So, this animation occurs.
所以,这个动画发生了。
<div id="splash"> <span id="name">random title
<div class="slider"></div>
</span>
</div>
.slider {
position: absolute;
display:block;
left: 0;
top:90%;
margin:0 auto;
height: 2px;
background-color: #000;
width: 0%;
transition: width 1s;
}
#name:hover > .slider {
width: 100%;
}
#splash {
width:100%;
height:100%;
background-color:#fff;
z-index:999999;
position:fixed;
}
#name {
color:#000;
font-family:'Arial-BoldMT';
font-weight:bold;
font-size:50px;
letter-spacing: -2px;
display:block;
left: 50%;
transform: translate(-50%, 0);
position:absolute;
top:40%;
margin:0 auto;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.