【问题标题】:Underline transition in Nav Menu导航菜单中的下划线过渡
【发布时间】:2015-12-13 20:55:27
【问题描述】:

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

到目前为止,我从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....但我想这毕竟没关系?

【问题讨论】:

  • 先别删,挺有意思的。
  • 为什么不固定宽度,左边:0,右边:0? or left: 50% and margin-left: -(fixed width) 数量?

标签: javascript html css css-transitions


【解决方案1】:

我想这就是你需要的

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;
}

jquery:

$(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

【讨论】:

    【解决方案2】:

    您只需要一些数学运算,因为您使用的是绝对定位元素(效果分类 div)。如果您使用任何 CSS 预处理器(我更喜欢 Sass),它会很容易完成。但是如果你想使用纯 CSS,你应该像这样手动对每个“nth-child”进行操作。 (你也可以很容易地用 javascript 做到这一点)

    CSS WAY
    

    您的布局宽度为 700 像素。并且其中的每个锚 (nav a) 标签都有 25% 的宽度。所以你的锚点宽度为 175px。 (700 * 0.25) 而且你需要固定宽度为 55px 的下划线。

    .ph-line-nav .effect {
        width: 55px;
        height: 2px;
        bottom: 5px;
        background: /*#00ABE8*/black;
    }
    

    如果您的活动锚是第一个,则您的下划线需要距开头 60 像素。

    (175 - 55) / 2 = 60

    60px(空格) + 55px(下划线) + 60px(空格)

    nav a:nth-child(1).active ~ .effect {
        left: 60px;
    }
    

    第二个必须使用 175 + 60 = 235px。

    nav a:nth-child(2).active ~ .effect {
        left: 235px;
    }
    

    你有公式,对吧?

    left = (nth-child - 1) * 175 + 60
    

    所以,

    nth-child(3) = (3 - 1) * 175 + 60 = 410

    nth-child(4) = (4 - 1) * 175 + 60 = 585


    更新:我看到你使用 jquery。然后;

    JQUERY WAY
    
    var layoutWidth = 700,
        underlineWidth = 55,
        menuCount = 4,
        menuWidth = layoutWidth / menuCount, //175px
        leftSpace = (menuWidth - underlineWidth) / 2; //60px
    
    $('.ph-line-nav .effect').css('width', underlineWidth);
    
    for(var i = 1; i < menuCount + 1; i++) {
        var left = (i - 1) * menuWidth + leftSpace; 
        $('nav a:nth-child(' + i + ').active ~ .effect').css('left', left)
    }
    

    当您更改 underlineWidth 和/或 menuCount 变量时,它们将以编程方式计算。

    【讨论】:

    • 你如何用javascript做到这一点?我试图考虑如何在 javascript 中计算这个,但我不擅长编写 javascript...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2020-08-11
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多