在我的主页上,我有一个带有ID的菜单,当我点击它时,它会滑动到相应的div并且它可以平滑运行。在URL中缓慢导航到ID
但是,当我不在我的主页上,我点击一个项目,我希望能够转到主页,然后滑动到该部分。
这是我现在使用的代码:
$('#mainMenu a').click(function(e){
e.preventDefault();
var div = $(this).attr('href');
if('<?=get_site_url()?>/' == '<?=get_permalink()?>')
{
$('html, body').animate({scrollTop:$(div).position().top}, 'slow');
}
else
{
window.location.href = '<?=get_site_url()?>/'+div;
}
});
这个做工精良,下一部分工程,但我不能让它滑动到ID。
if (window.location.hash != "") {
e.preventDefault();
$('html, body').animate({scrollTop:$(window.location.hash).position().top}, 'slow');
}
有没有一种方法可以防止浏览器直接跳到该部分,而是滑动到它?
http://stackoverflow.com/questions/3503559/prevent-default-hash-behavior-on-page -load – thefrontender