How to prevent with jQuery default browser behavior, jump to anchor if anchor is in the link when page loads?
var myLink = document.location.toStr开发者_Go百科ing();
if (myLink.match('#')) {
$('html, body').animate({ scrollTop: 0 }, 0); // OR
$(window).scrollTop();
}
Or.... can't find solution... searched... and searched
use return false / event.preventDefault on your links
var myLink = document.location.toString();
if (myLink.match('#')) {
$('html, body').animate({ scrollTop: 0 }, 0); // OR
$(window).scrollTop();
return false;
}
Or do you mean that you want o smooth scroll to the position of the ID you are refering to in your hash sign?
after reading your comments ignore this message.
maybe like this: http://jsbin.com/ajafor/4#hello
<a href="#" class="link">link Text</a>
$(function()
{
$('a.link').attr('href','javascript:void(0);')
});
or add manually
<a href="javascript:void(0);" class="link">link Text</a>
I did this by including localScroll and scrollTo, then simply call:
$(function () { $.localScroll(); });
精彩评论