简体   繁体   English

使导航悬停下划线响应浏览器调整大小

[英]Making navigation hover underline respond on browser resize

I currently have the following working navigation where an underline animates on link hover. 我目前有以下工作导航,其中下划线动画显示了链接悬停。 I noticed however, when I resize the browser, the underline doesn't reset to the correct position. 但是,我注意到,当我调整浏览器大小时,下划线不会重置为正确的位置。 I assumed that if I reused some JS from my mouse leave function and used it in a window resize function it would work just ok, but I keep getting an error. 我以为如果我从鼠标离开功能中重用了一些JS,并在窗口调整大小功能中使用了它,就可以正常工作,但是我一直遇到错误。 Any thoughts? 有什么想法吗?

https://codepen.io/anon/pen/MRQaMm https://codepen.io/anon/pen/MRQaMm

JS: JS:

$(".underline-nav").css("width", $("#one").width());
   $(".underline-nav").css("margin-left", $("#one").css("margin-left"));
   var unav = $(".underline-nav");
   $('nav a').mouseover(function(){
     $(".underline-nav").css("transition", "all ease 0.43s" );
       var position = $(this).position();
       unav.css({
         "width": $(this).width(),
         "margin-left": $(this).css("margin-left"),
         "left": position.left
       });
   })
   $('nav').mouseleave(function() {
               $(".underline-nav").css("transition", "all ease 0.7s" );
     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
       });
   })

//NEW BUT CAUSES ERROR
     var resizeTimer;
     $(window).resize(function() {
         clearTimeout(resizeTimer);
        $(".underline-nav").css("transition", "all ease 0.7s" );
        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
            });
     })

The value of $(this) is undefined inside of the anonymous resize function. $(this)的值在匿名resize函数中undefined If you replace $(this) with $('nav') , everything works as expected. 如果将$(this)替换$(this) $('nav') ,那么一切都会按预期进行。

In short, replace this… 简而言之,将其替换...

$(window).resize(function() {
  …
  var firstChild = $(this).find("a:first-child");
}

…with this …有了这个

$(window).resize(function() {
  …
  var firstChild = $("nav").find("a:first-child");
}

Demo 演示版

 $(".underline-nav").css("width", $("#one").width()); $(".underline-nav").css("margin-left", $("#one").css("margin-left")); var unav = $(".underline-nav"); $("nav a").mouseover(function() { $(".underline-nav").css("transition", "all ease 0.43s"); var position = $(this).position(); unav.css({ width: $(this).width(), "margin-left": $(this).css("margin-left"), left: position.left }); }); $("nav").mouseleave(function() { $(".underline-nav").css("transition", "all ease 0.7s"); 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 }); }); //NEW BUT CAUSES ERROR var resizeTimer; $(window).resize(function() { clearTimeout(resizeTimer); $(".underline-nav").css("transition", "all ease 0.7s"); var firstChild = $("nav").find("a:first-child"); var position = firstChild.position(); console.log("position", position); unav.css({ width: firstChild.width(), "margin-left": firstChild.css("margin-left"), left: position.left }); }); 
 html { background-color: blue; } body { margin: 0; } .underline-nav { background: tomato; height: .25rem; position: absolute; top: 6vh; margin-top: 1vw; transform: translatex(4vw); transition: all ease 0.37s; z-index: 0; } .mainnav { padding-top: 1vh; width: 100%; position: absolute; z-index: 4; font-size: 0.8vw; text-align: center; display: flex; justify-content: center; align-items: center; } nav img { padding-left: 4vw; height: 3.5vw; } a { color: #fff; text-decoration: none; font-weight: 700; padding-left: 4vw; letter-spacing: 0.4em; } .sectionone { height: 100vh; background: url('media/bg.jpg') no-repeat center center fixed; background-size: cover; color: #fff; font-family: 'Raleway'; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <nav> <div class="mainnav"> <a id="one" href="#">HOME</a> <a href="#">LINK 1</a> <a href="#">LINK 2</a> <img src="/media/Asset 1.png" alt="logo"> <a href="#">LINK 3</a> <a href="#">LINK 4</a> <a href="#">LINK 5</a> <div class="underline-nav"> </div> </div> </nav> 

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

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