Snake Eyes answer is the most dynamic since you don't need to encode every tag in js. All the client needs to do is use an HTML solution, better for managing CMS.
This little piece of code is all you need (well, of course, jQuery too)
$ (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;
}
}
});
});
Even if you have a stickymenu solution at the top of the page, pay attention to replacing #main_menu with the identifier or class of your menu:
$ (function () {
$ ('a [href * = #]: not ([href = #])'). click (function () {
if (location.pathname.replace (/ ^ \ //, '') == this.pathname.replace (/ ^ \ //, '') && location.hostname == this.hostname) {
var height_menu = $ ("# main_menu"). css ("height");
height_menu = parseInt (height_menu, 10);
var target = $ (this.hash);
target = target.length? target: $ ('[name =' + this.hash.slice (1) + ']');
if (target.length) {
$ ('html, body'). animate ({
scrollTop: target.offset (). top - height_menu
}, 1000);
return false;
}
}
});
});
source share