Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Jquery Smooth Scroll to a name and id Re: Jquery Smooth Scroll to a name and id

#111776
chrisdewar
Member

The reason it’s failing is because you are using this.hash as a selector. In the case of an element id it works, but you can’t select a name like that.

Try something like this:

$(document).ready(function() {  
$('a[href^="#"]').click(function() {  
var target = $(this.hash);  
if (target.length == 0) target = $('a[name="' + this.hash.substr(1) + '"]');  
if (target.length == 0) target = $('html');  
$('html, body').animate({ scrollTop: target.offset().top }, 500);  
return false;  
});  
});