简体   繁体   English

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

[英]Fixed NavBar on scroll using jQuery

I have my navbar on my page. 我的页面上有导航栏。 I had used CSS and jQuery to fix it at the top but when I am selecting any menu item in navbar it doesn't go to that section. 我曾经使用CSS和jQuery在顶部修复它,但是当我在导航栏中选择任何菜单项时,它不会转到该部分。 Give me the solution. 给我解决方法。

 <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> 

You have to define each section with the "id" attribute and then you have to use the "scrollTop" for those navigation which will scroll to those defined "id". 您必须使用“ id”属性定义每个部分,然后必须对导航使用“ scrollTop” ,这些导航将滚动到定义的“ id”。

HTML Code HTML代码

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

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

Jquery Code: 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();
});

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

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