开发者

How to prevent page "jump" if anchor is in the URL when page loads?

开发者 https://www.devze.com 2023-04-06 17:45 出处:网络
How to prevent with jQuery default browser behavior, jump to anchor if anchor is in the link when page loads?

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(); });
0

精彩评论

暂无评论...
验证码 换一张
取 消