【发布时间】:2015-12-13 20:55:27
【问题描述】:
我不知道这是不是一个愚蠢的问题。我只是想知道是否有任何更好或更新的教程可以让下划线在悬停在单击的链接上时从一个链接滑到另一个链接。
到目前为止,我从Underline transition in menu 找到了这个不错的教程,看起来它使用了来自Css-only Lavalamp-like Fancy Menu Effect 的教程。
我仍然对codepen 中的代码有一些问题
由于我能够指定下划线的宽度,因此我无法将下划线置于链接下方的中心,因为该位置是绝对的。我可以通过使用左侧元素将其居中,但在找到正确的数字之前需要反复试验。因此,如果可能的话,我想使用 margin: 0 auto 或 text-align: center 将下划线居中。
我想不出其他任何东西,但它是用于 wordpress 导航菜单的,所以我想知道您是否有任何提示。
HTML
<div class="width">
<nav class="ph-line-nav">
<a href="#">News</a>
<a href="#">Activities</a>
<a href="#">Search</a>
<a href="#">Time</a>
<div class="effect"></div>
</nav>
</div>
CSS
.width {
width: 700px;
margin: 0 auto;
}
nav {
margin-top:20px;
font-size: 110%;
display: table;
background: #FFF;
overflow: hidden;
position: relative;
width: 100%;
}
nav a {
text-align:center;
background: #FFF;
display: block;
float: left;
padding: 2% 0;
width: 25%;
text-decoration: none;
color: /*#555*/black;
transition: .4s;
color: /*#00ABE8*/ red;
/*border-right: 1px solid red;
border-left: 1px solid red;*/
}
/* ========================
Lava-lamp-line:
======================== */
.effect {
position: absolute;
left: 0;
transition: 0.4s ease-in-out;
}
nav a:nth-child(1).active ~ .effect {
left: 0%;
/* the middle of the first <a> */
}
nav a:nth-child(2).active ~ .effect {
left: 25%;
/* the middle of the second <a> */
}
nav a:nth-child(3).active ~ .effect {
left: 50%;
/* the middle of the third <a> */
}
nav a:nth-child(4).active ~ .effect {
left: 75%;
/* the middle of the forth <a> */
}
.ph-line-nav .effect {
width: /*55px*/ 25%;
height: 2px;
bottom: 5px;
background: /*#00ABE8*/black;
margin-left:/*-45px*/auto;
margin-right:/*-45px*/auto;
}
JS
$(document).ready(function() {
$('.ph-line-nav').on('click', 'a', function() {
$('.ph-line-nav a').removeClass('active');
$(this).addClass('active');
});
});
除非绝对必要,否则我并不真正关心 javascript,因此从某种意义上说,如果可能的话,我想使用纯 css 教程。无论如何,如果结果是一个愚蠢的问题,我会尽快删除这个问题......
更新:这有点像我要找的东西:example 但是,它有 javascript....但我想这毕竟没关系?
【问题讨论】:
-
先别删,挺有意思的。
-
为什么不固定宽度,左边:0,右边:0? or left: 50% and margin-left: -(fixed width) 数量?
标签: javascript html css css-transitions