【问题标题】:HTML, JS: Scroll to div unreliable with collapsing navbarHTML,JS:滚动到 div 折叠导航栏不可靠
【发布时间】:2017-06-02 18:40:06
【问题描述】:

我正在开发一个导航栏折叠并在向下滚动时变得不透明的网站。这导致了一个问题,即单击页面下方 div 的链接会导致该 div 的顶部转到页面顶部,而不是导航栏底部的下方。该网站位于rewindrl.uk。是否有某种方法可以在页面顶部抵消 div 滚动,使其排列整齐,或者在向下滚动时“预测”并考虑导航栏的高度? 完整源代码位于 github.com/rewindrl/rewindrl.github.io,因为该网站托管在 GitHub Pages 上。

导航代码:

<div id="sticky-anchor"></div>
<nav id="tf-menu" class="navbar navbar-default">
   <div class="container">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
             <span class="sr-only">Toggle navigation</span>
             <span class="icon-bar"></span>
             <span class="icon-bar"></span>
             <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand logo" href="#tf-home" style="padding:0px"><img itemprop="image" src="/img/Rewind Transparent.png" width=50 height=50 alt="Rewind Gaming Logo"></img></a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
         <ul class="nav navbar-nav navbar-right">
             <li><a href="#tf-home">Home</a></li>
             <li><a href="/apply">Apply</a></li>
                <li><a href="/signup">Tournaments</a></li>
          </ul>
      </div><!-- /.navbar-collapse -->
   </div><!-- /.container-fluid -->
</nav>

导航 CSS:

#tf-menu {
margin-bottom: 0;
background: transparent;
border: 0;
color: #fff !important;
padding: 2% 0;
transition: all 0.5s;
}
#sticky {
    padding: 0.5ex;
    background-color: #333;
    color: #fff;
    font-size: 2em;
    border-radius: 0.5ex;
}
#tf-menu.stick {
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 10000;
    background: #222222;
    padding: 1% 0;
}
#tf-menu.navbar-default .navbar-nav > li > a {
    color: #C5C5C5;
    text-transform: uppercase;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 1px;
}

#tf-menu.navbar-default .navbar-nav > li > a:hover, 
#tf-menu.navbar-default .navbar-nav > li > a:focus {
    color: #00D2FF;
    background-color: transparent;
}

滚动JS:

function main() {

(function () {
   'use strict';

   /*====================================
    Main Navigation Stick to Top when Scroll
    ======================================*/
    function sticky_relocate() {
      var window_top = $(window).scrollTop();
      var div_top = $('#sticky-anchor').offset().top;
      if (window_top > div_top) {
          $('#tf-menu').addClass('stick');
      } else {
          $('#tf-menu').removeClass('stick');
      }
  }

  $(function () {
      $(window).scroll(sticky_relocate);
      sticky_relocate();
  });


 $(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top - 70
        }, 1000);
        return false;
      }
    }
  });
});
}());
}
main();

我对JS没有太多经验,而且我使用的是CSS模板,所以在向我解释我有多愚蠢时请不要太深入! 提前致谢,

水桶工

【问题讨论】:

    标签: javascript jquery html css scroll


    【解决方案1】:

    我无法尝试,但我认为如果您将scrollTop: target.offset().top - 70 设置为更高的值(例如scrollTop: target.offset().top - 120)会有所帮助。

    - 70 是导航栏。它已经偏移了 70px

    试试看;)

    【讨论】:

    • 是的,我设法通过将偏移量设置为 175 来修复它。非常感谢! :)
    • 没问题! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 2019-11-04
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2014-12-24
    • 2017-06-06
    相关资源
    最近更新 更多