There is a smooth scrolling to an anchor for http: // test3 # e3 , you need to do what would work with http: // test3 / # e3 ,

$(document).ready(function(){ $('a[href^="#"]').click(function(){ //берем все ссылки атрибут href которых начинается с # if(document.getElementById($(this).attr('href').substr(1)) != null) { //на странице есть элемент с нужным нам id $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top }, 2000); // анимируем скролл к элементу } return false; }); }); 

    2 answers 2

    if on jQuery, for all anchor links on the current page:

     $(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 }, 1000); return false; } } }); }); 

      Subscribe to hashchange , do preventDefault and scroll.