簡體   English   中英

使用jQuery修復了滾動條上的NavBar

[英]Fixed NavBar on scroll using jQuery

我的頁面上有導航欄。 我曾經使用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> 

您必須使用“ 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();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM