Home › Forums › CSS › Jquery Smooth Scroll to a name and id › Re: Jquery Smooth Scroll to a name and id
October 13, 2012 at 5:02 am
#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;
});
});