javascript - 导航菜单中的下划线过渡

标签 javascript html css css-transitions

我不知道这是不是一个愚蠢的问题。我只是想知道是否有任何更好或更新的教程可以让下划线在悬停时从一个链接滑到另一个链接并停留在单击的链接上。

到目前为止,我从 Underline transition in menu 找到了这个不错的教程看起来它使用了 Css-only Lavalamp-like Fancy Menu Effect 中的教程.

我仍然对 codepen 中的代码有一些问题。

  1. 由于我能够指定下划线的宽度,所以我无法将下划线置于链接下方的中心位置,因为该位置是绝对的。我可以使用左侧元素将其居中,但在找到正确的数字之前需要反复试验。因此,如果可能的话,我想将下划线居中放置 margin: 0 auto 或 text-align: center。

  2. 除了 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....但我想这毕竟没关系吗?

最佳答案

我想这就是你需要的

HTML:

<div class="nav-wrap">
 <ul class="group" id="example-one">
    <li class="current_page_item"><a href="#">Home</a></li>
    <li><a href="#">Buy Tickets</a></li>
    <li><a href="#">Group Sales</a></li>
    <li><a href="#">Reviews</a></li>
    <li><a href="#">The Show</a></li>
    <li><a href="#">Videos</a></li>
    <li><a href="#">Photos</a></li>
    <li><a href="#">Magic Shop</a></li>
 </ul>
</div>

CSS:

    /* Example One */
#example-one { 
    margin: 0 auto; 
    list-style: none; 
    position: relative; 
    width: 960px; 
}
#example-one li { 
    display: inline-block;  
}
#example-one a { 
    color: #bbb; 
    font-size: 14px; 
    float: left;
    padding: 6px 10px 4px 10px;
    text-decoration: none;
    text-transform: uppercase;
}
#example-one a:hover { 
    color: black; 
}
#magic-line { 
    position: absolute;
    bottom: -2px; 
    left: 0; 
    width: 100px; 
    height: 2px; 
    background: #fe4902;
}
.current_page_item a { 
    color: black !important; 
}
.ie6 #example-one li, .ie7 #example-one li { 
    display: inline; 
}
.ie6 #magic-line {
    bottom: -3px;
}

J查询:

$(function() {

    var $el, leftPos, newWidth,
        $mainNav = $("#example-one");

    $mainNav.append("<li id='magic-line'></li>");
    var $magicLine = $("#magic-line");

    $magicLine
        .width($(".current_page_item").width())
        .css("left", $(".current_page_item a").position().left)
        .data("origLeft", $magicLine.position().left)
        .data("origWidth", $magicLine.width());

    $("#example-one li a").hover(function() {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();
        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }, function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });    
    });
});

JSFiddle

关于javascript - 导航菜单中的下划线过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622690/

相关文章:

html - 正确对齐 Div

html - css 中的多个图像对齐问题

javascript - 导航/子导航列表,如何在页面重新加载后为点击的项目提供事件类

javascript - 使用 jquery 更改链接样式和属性

javascript - 使用 jquery 在悬停时展开文本表单

javascript - 过滤回调传递的参数

javascript - jQuery:addClass 仅一次

html - 如何使 HTML 表格单元格在同一行中的其他单元格下方流动

javascript - Style.visibility 不隐藏元素

html - 自定义 Wordpress (Genesis) 主题