[英]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.