粘性导航栏,滚动到第一个元素不精确

我正在尝试制作一个在滚动到第一个元素(导航)(例如“新闻”)后保持在顶部的导航栏。它通过点击“新闻”来工作不精确。它向下滚动很多,再次点击“新闻”后,它再次向上滚动到正确的位置。奇怪的是,它只发生在导航栏的第一个元素上。我怎样才能解决这个问题?


//Sticky-NavBar

window.onscroll = function() {stickyNavBar()};

var navbar = document.getElementById("navbar");

var sticky = navbar.offsetTop;

function stickyNavBar() {

  if (window.pageYOffset >= sticky) {

    navbar.classList.add("sticky")

  } else {

    navbar.classList.remove("sticky");

  }

}


德玛西亚99
浏览 177回答 2
2回答

qq_花开花谢_0

您需要将其更改为 JS/JQuery,在我的例子中是 JQuery。加上这个脚本,你就会有更多的控制权,换成毫秒就可以做出动画了, 0。请注意,我正在从容器位置中减去导航栏高度。$(document).ready(function(){&nbsp; $("#navbar a").click(function(e) {&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; $('html, body').animate({&nbsp; &nbsp; &nbsp; scrollTop: $($.attr(this, 'href')).offset().top - $("#navbar").height()&nbsp; &nbsp; }, 0);&nbsp; });});//Sticky-NavBarwindow.onscroll = function() {stickyNavBar()};var navbar = document.getElementById("navbar");var sticky = navbar.offsetTop;function stickyNavBar() {&nbsp; if (window.pageYOffset >= sticky) {&nbsp; &nbsp; navbar.classList.add("sticky")&nbsp; } else {&nbsp; &nbsp; navbar.classList.remove("sticky");&nbsp; }}$(document).ready(function(){&nbsp; $("#navbar a").click(function(e) {&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; $('html, body').animate({&nbsp; &nbsp; &nbsp; scrollTop: $($.attr(this, 'href')).offset().top - $("#navbar").height()&nbsp; &nbsp; }, 0);&nbsp; });});/*Base*/.contentBody{&nbsp; background-color: white;}/*Sections*/html {&nbsp; scroll-behavior: smooth;}#header{}#news{}#products{background-color: aqua;}#services{}#customers{}#community{}#contact{}#about{}#footer{}/*Width Element*/.fitWidth{&nbsp; width: -webkit-fill-available;}.width100{&nbsp; width: 100%;}.fitContBoxLeft{&nbsp; display: inline-block;&nbsp; float: left;}.fitContBoxRight{&nbsp; display: inline-block;&nbsp; float: right;}/**//*Borders*/.roundedBorader{&nbsp; border-radius: 50%;}.roundedBox{&nbsp; &nbsp; border: 1px solid lightgrey;&nbsp; &nbsp; border-radius: 12px;}/*Div*/.headingBox{&nbsp; position: relative&nbsp; background-color: lightgrey;&nbsp; height: 268;}.news{&nbsp; background-color: purple;}.products{&nbsp; background-color: white;}.services{&nbsp; background-color: limegreen;}.customers{&nbsp; background-color: blue;}.pBox1{&nbsp; float:left;&nbsp; width: 24.7%;&nbsp; margin: 1px;&nbsp; border-radius: 5px;&nbsp; text-align: center;}.headBottomLine{&nbsp; height: 12%;&nbsp; width: 100%;&nbsp; background-color: grey;}.leftBox50{&nbsp; height: auto;&nbsp; float: left;&nbsp; background-color: white;&nbsp; width: 49%;&nbsp; margin: 2px;}.rightBox50{&nbsp; height: auto;&nbsp; float: left;&nbsp; background-color: white;&nbsp; width: 49%;&nbsp; margin: 2px;}.bodyBox{&nbsp; height: 200px;&nbsp; background-color: grey;}.aboutUs{&nbsp; height: 200px;&nbsp; background-color: white;}.footerBox{&nbsp; min-height: 300px;&nbsp; background-color: black;}/*Backgrounds*/.headBG{&nbsp; background-image: url("https://www.toptal.com/designers/subtlepatterns/patterns/double-bubble.png");&nbsp; background-size: cover;}/*Position*/.bottom0{&nbsp; bottom: 0;}.posRel{&nbsp; position: relative;}.posAbs{&nbsp; position: absolute;}/*Float*/.floatLeft{&nbsp; float: left;}.floatRight{&nbsp; float: right;}/*Alignment*/.justiText{&nbsp; text-align: justify;}.rightText{&nbsp; text-align: right;}.leftText{&nbsp; text-align: left;}.centerText{&nbsp; text-align: center;}.alignBox{&nbsp; display: flex;&nbsp; justify-content: center;}/*Padding*/.pAll-5{&nbsp; padding: 5%;}.pTop-8{&nbsp; padding-top: 8%;}.pTop-5{&nbsp; padding-top: 5%;}.pTop-2{&nbsp; padding-top: 2%;}.mTop-n3{&nbsp; margin-top: -3%;}.mTop-5{&nbsp; margin-top: auto;}.mTop-6{&nbsp; margin-top: 6%;}.pLine{&nbsp; width:100%;}.pLR{&nbsp; padding-left: 5%;&nbsp; padding-right: 5%;}.pBot-12{&nbsp; padding-bottom: 12%;}.pTop-10{&nbsp; padding-top: 10%;}.pTop-6{&nbsp; &nbsp; padding-top: 6%;}.pTop-1{&nbsp; padding-top: 2%;}.pLeft-30{&nbsp; padding-left: 30%;}.pLeft-25{&nbsp; padding-left: 25%;}.pLeft-20{&nbsp; padding-left: 20%;}.pLeft-10{&nbsp; padding-left: 10%;}.pLeft-5{&nbsp; padding-left: 5%;}.pRight-5{&nbsp; padding-right: 5%;}.pTop-4{&nbsp; padding-top: 4%;}.pAll-20{&nbsp; padding: 20px;}.pLTRB{&nbsp; &nbsp; padding-left: 15%;&nbsp; &nbsp; padding-top: 2%;&nbsp; &nbsp; padding-bottom: 5%;}.centerHeadLine{&nbsp; &nbsp; padding-bottom: 7%;}/*Colors*/.white{&nbsp; color: white;}.black{&nbsp; color: black;}.grey{&nbsp; color: grey;}.lightgrey{&nbsp; color: lightgrey;}div.c{&nbsp; color: lightgrey;}div.b{&nbsp; color: white;}div.a{&nbsp; color: black;}.bgCol-black{&nbsp; background-color: black;}.bgCol-grey{&nbsp; background-color: grey;}.bgCol-purple{&nbsp; background-color: #8e42c7;}.bgCol-white{&nbsp; background-color: white;}.bgCol-lightgrey{&nbsp; background-color: lightgrey;}.bgCol-limegreen{&nbsp; background-color: limegreen;}.bgLightGreyX{&nbsp; background-color: #d5d2d670;}/*Filter styles*/.saturate { filter: saturate(3); }.grayscale { filter: grayscale(100%); }.contrast { filter: contrast(160%); }.brightness { filter: brightness(0.25); }.blur { filter: blur(3px); }.invert { filter: invert(100%); }.sepia { filter: sepia(100%); }.huerotate { filter: hue-rotate(180deg); }.rss.opacity { filter: opacity(50%); }/*Heading Colors*/h1.a{&nbsp; color: white;}h1.b{&nbsp; color: black;}h5.a{&nbsp; color: white;}h4.b{&nbsp; color: black;}h4.a{&nbsp; color: white;}h5.b{&nbsp; color: black;}/*Body*/body {&nbsp; margin: 0;&nbsp; font-size: 20px;&nbsp; font-family: Arial, Helvetica, sans-serif;}/*NavBar*/.navbar {&nbsp; overflow: hidden;&nbsp; background-color: #333;}.navbar a {&nbsp; float: left;&nbsp; font-size: 16px;&nbsp; color: white;&nbsp; text-align: center;&nbsp; padding: 14px 16px;&nbsp; text-decoration: none;}/*Nav-Dropdown*/.dropdown {&nbsp; float: left;&nbsp; overflow: hidden;}.dropdown .dropbtn {&nbsp; font-size: 16px;&nbsp;&nbsp;&nbsp; border: none;&nbsp; outline: none;&nbsp; color: white;&nbsp; padding: 14px 16px;&nbsp; background-color: inherit;&nbsp; font-family: inherit;&nbsp; margin: 0;}.navbar a:hover, .dropdown:hover .dropbtn {&nbsp; background-color: #ea8013;}.dropdown-content {&nbsp; display: none;&nbsp; position: fixed;&nbsp; background-color: #333;&nbsp; min-width: 160px;&nbsp; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);&nbsp; z-index: 1;}.dropdown-content a {&nbsp; float: none;&nbsp; color: white;&nbsp; padding: 12px 16px;&nbsp; text-decoration: none;&nbsp; display: block;&nbsp; text-align: left;}.dropdown-content a:hover {&nbsp; background-color: #94a25c;}.dropdown:hover .dropdown-content {&nbsp; display: block;}/* The sticky class*/.sticky {&nbsp; position: fixed;&nbsp; top: 0;&nbsp; width: 100%;}.sticky + .content {&nbsp; padding-top: 0px;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><!---------------------------------------------------Header---------------------------------------------------><div class='headBG posRel' id='home'>&nbsp; <div class='pTop-4 pLeft-10'>&nbsp; &nbsp; <img class='floatLeft roundedBorader' src='https://pbs.twimg.com/media/EaZSuXXXkAAMtJ5?format=jpg&name=360x360' height="150" width="150"/>&nbsp; &nbsp; <div class='centerHeadLine pLeft-25'>&nbsp; &nbsp; &nbsp; <h1 class='b'>Welcome friends</h1>&nbsp; &nbsp; &nbsp; <h5 class='b'><u>The spot where we test some html, css and js</u></h5>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <!--NAV-->&nbsp; <div class="navbar fitWidth" id="navbar">&nbsp; &nbsp; &nbsp; <a class="active" href="#home">Home</a>&nbsp; &nbsp; &nbsp; <a class="" href="#news">News</a>&nbsp; &nbsp; &nbsp; <a class="" href="#products">Products</a>&nbsp; &nbsp; &nbsp; <a class="" href="#services">Services</a>&nbsp; &nbsp; &nbsp; <a class="" href="#customers">Customers</a>&nbsp; &nbsp; &nbsp; <a class="" href="#community">Community</a>&nbsp; &nbsp; &nbsp; <a class="" href="#contact">Contact</a>&nbsp; &nbsp; <div class="dropdown">&nbsp; &nbsp; &nbsp; <button class="dropbtn">About&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-caret-down"></i>&nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; <div class="dropdown-content">&nbsp; &nbsp; &nbsp;<!-- //TODO: Modals als POP up einbauen, mit jeweiligen content -->&nbsp; &nbsp; &nbsp; &nbsp; <a href="#">Link 1</a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#">Link 2</a>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#">Link 3</a>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp;&nbsp; </div></div><!---------------------------------------------------Content--------------------------------------------------><div class='contentBody'>&nbsp; <!--News-->&nbsp; &nbsp;<div class='news' id="news">&nbsp; &nbsp; &nbsp; <h1 class='a centerText pTop-5 mTop-5'>News</h1>&nbsp; &nbsp; &nbsp; <h5 class='a centerText'>Stay tunned with our News</h5>&nbsp; &nbsp; <div class='alignBox'>&nbsp; &nbsp; &nbsp; <div class='leftBox50 alignBox roundedBox'>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <div class='centerText'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4>Lorem ipsum</h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class='rightBox50 alignBox roundedBox'>&nbsp; &nbsp; &nbsp; &nbsp; <div class='centerText'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h4>Lorem ipsum</h4>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp;&nbsp; &nbsp;</div>&nbsp; &nbsp; <div class='alignBox pAll-20' id=''>&nbsp; &nbsp; &nbsp; <form action="/action_page.php">&nbsp; &nbsp; &nbsp; &nbsp; <label class='white' for="lname">Newsletter:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="lname" name="lname">&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="Submit">&nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; &nbsp;</div>&nbsp; <!--//News-->&nbsp;&nbsp;&nbsp; <!--Products-->&nbsp; <div class='products' id='products'>&nbsp; &nbsp; <h1 class='centerText pTop-6 mTop-5'>Products</h1>&nbsp; &nbsp; <h5 class='b centerText'>Stay tunned with our products</h5>&nbsp; &nbsp; <div class='leftText pAll-20'>&nbsp; &nbsp; &nbsp; &nbsp; <h4>Lorem ipsum</h4>&nbsp; &nbsp; &nbsp; &nbsp; <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class='pAll-20'>&nbsp; &nbsp; &nbsp; <div class='white pBox1 bgLightGreyX'>&nbsp; &nbsp; &nbsp; &nbsp; <img class='pTop-5' src='https://via.placeholder.com/150'/><br>&nbsp; &nbsp; &nbsp; &nbsp; <p>Product #1</p>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class='white pBox1 bgLightGreyX'>&nbsp; &nbsp; &nbsp; &nbsp; <img class='pTop-5' src='https://via.placeholder.com/150'/><br>&nbsp; &nbsp; &nbsp; &nbsp; <p>Product #2</p>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class='white pBox1 bgLightGreyX'>&nbsp; &nbsp; &nbsp; &nbsp; <img class='pTop-5' src='https://via.placeholder.com/150'/><br>&nbsp; &nbsp; &nbsp; &nbsp; <p>Product #3</p>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class='white pBox1 bgLightGreyX'>&nbsp; &nbsp; &nbsp; &nbsp; <img class='pTop-5' src='https://via.placeholder.com/150'/><br>&nbsp; &nbsp; &nbsp; &nbsp; <p class='black'>Product #4</p>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; <div class='pTop-10 pLR'>&nbsp; &nbsp; &nbsp; <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus e</p>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <!--//Products-->&nbsp; <!--Services-->&nbsp; <div class='services' id='services'>&nbsp; &nbsp; <h1 class='centerText pTop-5 mTop-5'>tile#1</h1>&nbsp; &nbsp; <h5 class='b centerText'>Stay tunned with our Services</h5>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class='pLeft-5 fitContBoxLeft roundedBox'>&nbsp; &nbsp; &nbsp; <img class='floatLeft' src='https://via.placeholder.com/50'/><br>&nbsp; &nbsp; &nbsp; <div class='floatLeft pLeft-5'>tile#2</div>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class='pRight-5 fitContBoxRight roundedBox'>&nbsp; &nbsp; &nbsp; <img class='floatRight' src='https://via.placeholder.com/50'/><br>&nbsp; &nbsp; &nbsp; <div class='floatRight pRight-5'>tile#3</div>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class='pLeft-5 fitContBoxLeft roundedBox'>&nbsp; &nbsp; &nbsp; <img class='floatLeft' src='https://via.placeholder.com/50'/><br>&nbsp; &nbsp; &nbsp; <div class='floatLeft pLeft-5'>tile#4</div>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class='pRight-5 fitContBoxRight roundedBox'>&nbsp; &nbsp; &nbsp; <img class='floatRight' src='https://via.placeholder.com/50'/><br>&nbsp; &nbsp; &nbsp; <div class='floatRight pRight-5'>tile#5</div>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <h4 class='centerText pTop-5'>asdfadsf asdf&nbsp; asdf asd f asdfvices</h4>&nbsp; &nbsp; <div class=''></div>&nbsp; </div>&nbsp; <!--//Services-->&nbsp; <!--Customers-->&nbsp; <div class='customers' id='customers'>&nbsp; &nbsp; <h1 class='a centerText'>Customers</h1>&nbsp; &nbsp; <div class=''></div>&nbsp; &nbsp; <div class=''></div>&nbsp; &nbsp; <p>jgfjhgfghjf</p>&nbsp; </div>&nbsp; <!--//Customers-->&nbsp; <!--Community-->&nbsp; <div class='' id='Community'>&nbsp; &nbsp; <h1 class='a centerText'>Community</h1>&nbsp; &nbsp; <div class=''></div>&nbsp; &nbsp; <div class=''></div>&nbsp; </div>&nbsp; <!--//Community-->&nbsp; <!--Contact-->&nbsp; <div class='' id='Contact'>&nbsp; &nbsp; <h1 class='a centerText'>Contact</h1>&nbsp; &nbsp; <div class=''></div>&nbsp; &nbsp; <div class=''></div>&nbsp; </div>&nbsp; <!--//Contact-->&nbsp; <!--About-->&nbsp; <div class='aboutUs'>&nbsp; &nbsp; <div></div>&nbsp; </div>&nbsp; <!--//About--></><!---------------------------------------------------Footer---------------------------------------------------><div class='footerBox'>&nbsp; <div></div></div>

天涯尽头无女友

当您使用 转换元素时position: fixed;,该元素将停止发生。因此,您需要补偿导航栏不再占用的高度不足。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript