简体   繁体   English

垂直导航栏移至顶部

[英]A Vertical Navigation Bar Moving to The Top

I'm trying to make my vertical navigation bar to move to the top when the user scrolls (the original position is not at the top). 我试图使我的垂直导航栏在用户滚动时移动到顶部(原始位置不在顶部)。 I only know HTML, CSS and JavaScript, so I don't know jQuery. 我只了解HTML,CSS和JavaScript,所以不了解jQuery。 Here is the code for the navigation bar: 这是导航栏的代码:

Is there something wrong with the class or id names or is it the JavaScript code? 类或ID名称是否有问题,或者是JavaScript代码?

 var body = document.getElementsByTagName("body")[0]; var navigation = document.getElementById("navigation"); window.addEventListener("scroll", navigationFixing()); function navigationFixing() { if (body.scrollTop > navigation.getBoundingClientRect().bottom) {navigation.className = "fixedNavigation"; } else { navigation.className = "notFixedNavigation"; } } 
 #navigation {list-style-type: none; width: 15%; margin: 0px; padding: 0px; font-size: 35px; border: 1px solid; height: 100%; background-color: #d6d6c2; overflow: auto; position: absolute; } .navigationbar { border-bottom: 1px solid black; } .navigationbar a {display: block; background-color: #C0C0C0; padding-left: 10px; padding-bottom: 16px; text-decoration: none; color: #ffffff; } .navigationbar a:hover {background-color: #404040; color: white;} .navigationbar a.nuvarande {background-color: black; } .fixedNavigation { position: fixed; top: 0; left: 0; } .notFixedNavigation { position: absolute; } 
 <ul id="navigation" class="notFixedNavigation"> <li class="navigationbar"> <a href="index.html">Home</a> </li> <!---------------DATOR------------------- <li class="navigationbar"> <a href="play.html">Play</a> </li> ----------------------------------------> <li class="navigationbar"> <a href="" class="nuvarande">Rules</a> </li> <li class="navigationbar"> <a href="tactics.html">Tactics</a> </li> <li class="navigationbar"> <a href="contact.html">Contact</a> </li> </ul> 

You care calling your event handler immediately, instead of attaching it to the listener. 您需要立即调用事件处理程序,而不是将其附加到侦听器。 Remove the parens. 拆下括号。

window.addEventListener("scroll", navigationFixing);

In addition, navigation.getBoundingClientRect().bottom will change when the position changes. 另外,位置更改时, navigation.getBoundingClientRect().bottom也将更改。 Better to set it outside the function. 最好在功能之外进行设置。

var pos = navigation.getBoundingClientRect().bottom;
function navigationFixing() {
  if (body.scrollTop > pos) {...}
}

Also note from @bestinamir, your CSS is being overwritten. 另请注意@bestinamir,您的CSS已被覆盖。 It needs some work, but you can start with: 它需要一些工作,但是您可以从以下内容开始:

.fixedNavigation {
  position: fixed !important;
  top: 0;
  left: 0;
}

@mhatch Thanks for the answer. @mhatch感谢您的回答。 I did what you what you said and it still did not work. 我按照您说的做了,但仍然没有用。 I even tried to change the JavaScript code to this instead. 我什至尝试将JavaScript代码更改为此。

 var body = document.getElementsByTagName("body")[0]; var navigation = document.getElementById("navigation"); window.addEventListener("scroll", navigationFixing); var pos = navigation.getBoundingClientRect().bottom; function navigationFixing() { /*if (body.scrollTop > pos) {navigation.className = "fixedNavigation"; } else { navigation.className = "notFixedNavigation"; }*/ if (body.scrollTop > pos) {navigation.classList.remove("NotFixedNavigation"); navigation.classList.add("fixedNavigation") } else { navigation.classList.remove("fixedNavigation"); navigation.classList.add("notFixedNavigation") } } 
 #navigation {list-style-type: none; width: 15%; margin: 0px; padding: 0px; font-size: 35px; border: 1px solid; height: 100%; background-color: #d6d6c2; overflow: auto; /*position: absolute*/ } .navigationbar { border-bottom: 1px solid black; } .navigationbar a {display: block; background-color: #C0C0C0; padding-left: 10px; padding-bottom: 16px; text-decoration: none; color: #ffffff; } .navigationbar a:hover {background-color: #404040; color: white;} .navigationbar a.nuvarande {background-color: black; } .fixedNavigation { position: fixed; top: 0; left: 0; } .notFixedNavigation { position: absolute; } 
 <ul id="navigation" class="notFixedNavigation"> <li class="navigationbar"> <a href="index.html">Home</a> </li> <li class="navigationbar"> <a href="" class="nuvarande">Rules</a> </li> <li class="navigationbar"> <a href="tactics.html">Tactics</a> </li> <li class="navigationbar"> <a href="contact.html">Contact</a> </li> </ul> 

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

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