javascript - WP menu is not working as expected -


super newb question here, brian not working tonight. i'm using provided js script on wordpress site make single-page website menu scroll relevant section.

i have menu options 'home' 'people' 'info' 'contact'

these links structured so: "http://sample.com/#home"

<!-- navigation menu --> <div class="collapse navbar-collapse" id="navigation">   <ul id="menu-menu-1" class="nav navbar-nav navbar-right"><li id="menu-item-15"   class="menu-item menu-item-type-custom menu-item-object-custom menu-item-15"> <a href="http://sample.com/#home">home</a></li> <li id="menu-item-51" class="menu-item menu-item-type-custom menu-item-object-custom menu-        item-51"> <a href="http://sample.com/#people">people</a></li> <li id="menu-item-14" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-14">  <a href="http://sample.com/#info">info</a></li>  <li id="menu-item-54" class="menu-item menu-item-type-custom menu-item-object-custom  menu-item-54">  <a href="http://sample.com/#contact">contact</a></li>  </ul>        </div> <!-- end navigation menu --> 

the js follows below.

 //navigation scrolling  $('#navigation a[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     }, 1000);     return false;   } } }); 

the problem: when clicking menu-links, page reloads , shows relevant section.

i menu items scroll relevant section instead.

thanks.

tl;dr menu items reload page instead of scrolling relevant section. js above.

just prevent default functionality of anchor tag using event.preventdefault(),

 $('#navigation a[href*=#]').click(function(e) {    e.preventdefault();    //rest of code 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -