繁体   English   中英

导航菜单中的下划线过渡

[英]Underline transition in Nav Menu

我不知道这是一个愚蠢的问题。 我只是想知道是否有更好的或更新的教程,以便当鼠标悬停在下划线并保持点击链接时,下划线可以从一个链接滑到另一个链接。

到目前为止,我在菜单中的Underline transition中找到了一个不错的教程,看起来它使用的是仅Css式Lavalamp风格菜单效果中的教程。

我仍然在Codepen中使用该代码存在一些问题

  1. 由于我可以指定下划线的宽度,因此由于位置是绝对的,因此无法将下划线居中于链接下方。 我可以通过使用left元素将其居中,但是在找到正确的数字之前,它需要反复试验。 因此,我想尽可能将下划线居中以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教程。 无论如何,如果事实证明这是一个愚蠢的问题,我会尽快删除。

更新:这是我一直在寻找的: 示例,但是,它具有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;
}

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

您只需要那里的一些数学运算,因为您使用的是绝对定位的元素(效果分类div)。 如果您使用任何CSS预处理器(我更喜欢Sass),则将很容易做到。 但是,如果要使用纯CSS,则应像这样对每个“第n个子对象”手动进行操作。 (您也可以使用javascript轻松做到这一点)

CSS WAY

您的布局宽度为700px。 并且其中每个锚点(nav a)的宽度为25%。 因此您的锚点宽度为175px。 (700 * 0.25),并且需要具有固定宽度的下划线,即55px。

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

如果您的主动锚位于第一位,则下划线必须从头开始为60px。

(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变量时,将以编程方式计算它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM