简体   繁体   English

当我向上和向下滚动时,导航栏品牌不会被隐藏

[英]Navbar brand doesn't get hidden when I scroll up and down

I'm using bootstrap for my navbar and the navbar brand is an image.我正在为我的导航栏使用引导程序,导航栏品牌是一个图像。 I use a bit of javascript to make the navbar auto hide and show up based on scrolling.我使用了一些 javascript 使导航栏自动隐藏并基于滚动显示。 But the problem is my navbar brand(image) , still sticks to the top of the website without disappearing when scrolling unlike other navbar links.但问题是我的导航栏品牌(图片),与其他导航栏链接不同,滚动时仍然粘在网站顶部而不会消失。 I couldn't find a solution yet.我还没有找到解决方案。

html html

<nav id="navbar" class="navbar navbar-expand-lg">
            <a  class="navbar-brand" href="#"><img class="lg" src="css/images/logo.png" alt=""></a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
            </button>
        
            <div class="collapse navbar-collapse nav-links" id="navbarSupportedContent">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item active">
                    <a class="nav-link" href="#">Home</a>
                    </li>
                    <li class="nav-item">
                    <a class="nav-link" href="#">About</a>
                    </li>
                    <li class="nav-item">
                    <a class="nav-link" href="#">Projects</a>
                    </li>
                    <li class="nav-item">
                    <a class="nav-link" href="#">Contact</a>
                    </li>
                </ul>
            </div>
        </nav>

css css

.lg {
  width: 70px;
  height: 70px;
}

.nav-item {
  padding-left: 1rem;
  padding-right: 1rem;
}

.nav-link {
  font-size: 18px;
  font-weight: 500;
}


#navbar {
  background-color: #0a192f;
  position: fixed;
  top: 0;
  width: 100%;
  height: 90px;
  display: block;
  transition: top 0.3s;
}

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 15px;
  text-decoration: none;
  font-size: 17px;
}

#navbar a:hover {
  background-color: #ddd;
  color: black;
}

javascript javascript

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
  var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementById("navbar").style.top = "0";
  } else {
    document.getElementById("navbar").style.top = "-50px";
  }
  prevScrollpos = currentScrollPos;
}

Please help me with this issue.请帮我解决这个问题。 Thanks very much!非常感谢!

Are you executing your javascript code in the document.onLoad event?您是否在 document.onLoad 事件中执行您的 javascript 代码? I think your issue is我认为你的问题是

If you're using jQuery, make sure you're running this piece of code in the如果您使用的是 jQuery,请确保您在

$(function() { ... });

block.堵塞。 If you're using vanilla JS, you should do it in the onLoad event:如果您使用的是 vanilla JS,则应该在 onLoad 事件中执行此操作:

window.addEventListener('load', function(event) {
   // your code here 
});

I did that a while back with jQuery, but I do it with classes.不久前我用 jQuery 做到了这一点,但我用类来做到这一点。 If it exceeds a given threshold (60px - the height of my header) then add a given class, else remove it.如果它超过给定的阈值(60px - 我的标题的高度),则添加一个给定的类,否则将其删除。

$(function () {

      $(window).on('scroll', function () {
        checkForVerticalScrolling.call(this);
      });

      function checkForVerticalScrolling() {
        if ($(this).scrollTop() > 60) {
          $(".header-component").addClass('header-component-bg');
          $(".header-menu").addClass('header-menu-bg');
        } else {
          $(".header-component").removeClass('header-component-bg');
          $(".header-menu").removeClass('header-menu-bg');
        }
      }

      checkForVerticalScrolling.call(this);
    });

Hope that's useful.希望这是有用的。

I found the issue.我发现了这个问题。

} else {
    document.getElementById("navbar").style.top = "-50px";
}

It happened because of this line of code.它发生是因为这行代码。 My navbar doesn't get disappeared completely , half of it still sticks to the top, but I couldn't see it because the navbar background color was set to transparent.我的导航栏没有完全消失,它的一半仍然粘在顶部,但我看不到它,因为导航栏背景颜色设置为透明。 so I changed "-50px" to "-90px" , now the navbar gets hidden completely.所以我将"-50px"更改为"-90px" ,现在导航栏完全隐藏了。 Thanks for taking your time to answer my question.感谢您抽出宝贵时间回答我的问题。

暂无
暂无

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

相关问题 我试图让我的导航栏在我向下滚动时向上缩回,然后在我向上滚动时再次出现,但它没有做任何事情 - I'm trying to make my navbar retract upwards when I scroll down and then appear again when I scroll up, but it doesn't do anything 我试图在用户向下滚动时修复我的导航栏,但是当我向下滚动时它不会停留 - I'm trying to make my navbar fixed when the user scrolls down, but when I scroll down it doesn't stay bootstrap 4 - 导航栏品牌标志在向下滚动时不会改变 - bootstrap 4 - navbar brand logo not change in scroll down 单击品牌时,引导程序导航栏未设置活动页面 - bootstrap navbar doesn't set the active page when the brand is clicked 我希望我的导航栏在我向下滚动时消失并在我向上滚动时出现。 导航栏是固定的。 怎么了? - I want my navbar to disappear when I scroll down and appear when I scroll up. The navbar is fixed. What is wrong? 向下滚动时调整导航栏徽标的大小,Javascript 不起作用 - Resize navbar logo on scroll down, Javascript doesn't work CodeMirror 没有完全向下滚动,Cursor 隐藏在 div 后面 - CodeMirror doesn't scroll down completely, Cursor is hidden behind div 向下滚动时在Navbar中过渡 - Transition in Navbar when Scroll Down 当导航栏固定在滚动条顶部时,添加品牌徽标和li - Add brand logo and a li when navbar fixed top on scroll Vue 3:我无法向下或向上滚动 - Vue 3: I can't scroll down or up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM