I want to achieve smooth scrolling to an anchor with jQuery. I found some solutions, however, they're all based on the fact that the link to the anchor looks like this:
<a href="#anchor-id">TEST</a>
In my scenario, the links look like this:
<a href="some-page#anchor-id">TEST</a>
I have a global navigation-bar that has the link to "some-page" and hovering the menu-entry opens a dropdown inlcuding the links to the anchors on that page.
I found the following script online which works for regular anchor links <a href="#...">
:
$('a[href^="#"]').bind('click.smoothscroll', function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $taeget.offset().top-60
}, 4000, 'swing', function () {
window.location.hash = target;
});
});
The link to the fiddle: http://jsfiddle.net/2nT8M/5/
The fiddle doesn't seem to work, however the code does work on my website...
My questions:
- How can I get the script to work in the fiddle (obviously)?
- Is it possible to use links that include more than just the anchor ID, as needed in my scenario?