How to prevent the page from “jumping” if the link is specified in the URL when the page loads?

How to prevent jQuery from default browser behavior, go to the link, if the link is in the link when the page loads?

var myLink = document.location.toString();

if (myLink.match('#')) {
 $('html, body').animate({ scrollTop: 0 }, 0); // OR
 $(window).scrollTop();
}

Or .... can't find a solution ... search ... and search

+5
source share
3 answers

use return false / event.preventDefault for 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 to smooth the scroll to the position of the identifier that you refer to in your hash sign?

after reading your comments ignore this post.

could be something like this: http://jsbin.com/ajafor/4#hello

+4
source
<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>
+1

I did this by turning on localScroll and scrollTo , then just call:

$(function () { $.localScroll(); });
0
source

All Articles