简体   繁体   English

在导航中修改jQuery滑动下划线

[英]Modifying jQuery sliding underline in navigation

I have the following sliding underline element under my navigation. 我的导航下有以下滑动下划线元素。 The idea is that when a link is hovered, the underline slides over to that element. 想法是,当链接悬停时,下划线会滑到该元素上。 See codepen: https://codepen.io/lucasengnz/pen/eQaQxy 参见codepen: https ://codepen.io/lucasengnz/pen/eQaQxy

The issue I have is that when the nav is not being used I want the underline to slide back to the first link. 我的问题是,当不使用导航时,我希望下划线滑回到第一个链接。 I have no clue on how to do this, as I am quite new to using javascript and jQuery . 我不知道如何执行此操作,因为我对使用javascriptjQueryjQuery Any pointers? 有指针吗?

 $(".underline-nav").css("width", $("#one").width()); $(".underline-nav").css("margin-left", $("#one").css("margin-left")); $('nav a').hover(function() { $(".underline-nav").css("width", $(this).width()); $(".underline-nav").css("margin-left", $(this).css("margin-left")); var position = $(this).position(); $(".underline-nav").css("left", position.left); }) 
 .underline-nav { background: tomato; height: .25rem; position: absolute; top: 1.8em; transition: all ease 0.3s; } nav { font-size: 1.85em; } nav a { text-decoration: none; color: #000; float: left; margin-top: 1vh; } #one { margin-left: 2vw; } .floatright { float: right; padding-right: 3vw; } .floatright a { margin-left: 4vw; } 
 <div class="container"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav> <a id="one" href="#">one</a> <div class="floatright"> <a id="tt" href="#">two</a> <a href="#">three</a> <a href="#">four</a> <a href="#">five</a> </div> <div class="underline-nav"> </div> </nav> </div> 

Thanks for any help 谢谢你的帮助

You can do it like this: 您可以这样做:

    $(".underline-nav").css("width", $("#one").width());
    $(".underline-nav").css("margin-left", $("#one").css("margin-left"));
    var unav = $(".underline-nav");
    $('nav a').mouseover(function(){
        var position = $(this).position();
        unav.css({
          "width": $(this).width(),
          "margin-left": $(this).css("margin-left"),
          "left": position.left
        });
    })
    $('nav').mouseleave(function() {
      var firstChild = $(this).find('a:first-child');
      var position = firstChild.position();
        unav.css({
          "width": firstChild.width(),
          "margin-left": firstChild.css("margin-left"),
          "left": position.left
        });
    })

.hover Bind one or two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements. .hover将一个或两个处理程序绑定到匹配的元素,以在鼠标指针进入和离开元素时执行。

So you can underline the first element when mouse pointer leaves the element. 因此,当鼠标指针离开元素时,您可以在第一个元素下划线。

 $(".underline-nav").css("width", $("#one").width()); $(".underline-nav").css("margin-left", $("#one").css("margin-left")); $('nav a').hover(function() { $(".underline-nav").css("width", $(this).width()); $(".underline-nav").css("margin-left", $(this).css("margin-left")); var position = $(this).position(); $(".underline-nav").css("left", position.left); }, function () { // on leave , revert to first $(".underline-nav").css("width", $("#one").width()); $(".underline-nav").css("margin-left", $("#one").css("margin-left")); $(".underline-nav").css("left", $("#one").position().left); } ) 
 .underline-nav { background: tomato; height: .25rem; position: absolute; top: 1.8em; transition: all ease 0.3s; } nav { font-size: 1.85em; } nav a { text-decoration: none; color: #000; float: left; margin-top: 1vh; } #one { margin-left: 2vw; } .floatright { float: right; padding-right: 3vw; } .floatright a { margin-left: 4vw; } 
 <div class="container"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav> <a id="one" href="#">one</a> <div class="floatright"> <a id="tt" href="#">two</a> <a href="#">three</a> <a href="#">four</a> <a href="#">five</a> </div> <div class="underline-nav"> </div> </nav> </div> 

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

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