使用jQuery修复了滚动条上的NavBar

时间:2018-09-17 05:54:02

标签: javascript jquery html5 twitter-bootstrap css3

我的导航栏在页面上。我曾经使用CSS和jQuery在顶部修复它,但是当我在导航栏中选择任何菜单项时,它不会转到该部分。给我解决方法。

<header id="index-banner">
  <nav id="navigation" class="banner-header z-depth-0	">
    <ul class="left hide-on-med-and-down">
      <li><a href="http://localhost/project/" class="head"><span>HOME</span></a></li>
      <li><a href="#location" class="head" id="loc"><span>LOCATIONS</span></a>
        <li><a href="faq.php" class="head"><span>FAQ</span></a></li>
      </li>
    </ul>
    <a href="" class="center brand-logo"><img src="img/logo/50.png" class="banner-logo" id="mylogo"></a>
    <ul class="right hide-on-med-and-down">
      <span><li><a href="#menu" class="head">MENU</a></li></span>
      <span><li><a href="contact.php" class="head">CONTACT US</a></li></span>
      <li><img src="img/tunnsingh/face.png" id="tunnsinghface"></li>
      <li>
        <h5 id="call" class="head"><span>989898XXXX</span></h5>
      </li>
    </ul>
    <a href="#" data-activates="slide-out" class="hide-on-large-only button-collapse">&nbsp&nbsp<i class="fa fa-bars"></i></a>
    <div id="slide-out" class="side-nav hide-on-large-only">
      <img src="img/logo/50.png" id="side-nav-logo">
      <img src="img/divider/green.png" class="divide">
      <ul>
        <span><li><a href="http://localhost/project">HOME</a></li></span>
        <span><li><a href="#location" id="locm">LOCATIONS</a></li></span>
        <span><li><a href="#menu">MENU</a></li></span>
        <span><li><a href="faq.php">FAQ</a></li></span>
        <span><li><a href="contact.php">CONTACT US</a></li></span>
      </ul>
    </div>
  </nav>
  <img src="img/banner.jpg" class="hide-on-large-only" id="index-banner">
</header>

1 个答案:

答案 0 :(得分:0)

您必须使用“ id”属性定义每个部分,然后必须使用“ scrollTop” 进行导航,这些导航将滚动到定义的“ id”。

HTML代码

<a href="#target-element" class="scroll_to">Scroll to Target Element</a>

<section id=”target-element”>Target Element Content</section>

jQuery代码:

jQuery('.scroll_to').click(function(e){
    var jump = $(this).attr('href');
    var new_position = $(jump).offset();
    $('html, body').stop().animate({ scrollTop: new_position.top }, 500);
    e.preventDefault();
});