2013-03-07 42 views
2

我有一个固定的菜单,使用jQuery来平滑滚动到一个锚点,工作正常 - 但是当我使用脚本锚点链接不再出现在URL中。有任何想法吗?获取锚#id url回到jquery

该网站是http://www.julianvanmil.com

听到我正在使用的代码;

<script> 
jQuery(document).ready(function($) { 

    $(".scroll").click(function(event){  
     event.preventDefault(); 
     $('html,body').animate({scrollTop:$(this.hash).offset().top}, 400); 
    }); 
}); 
$(function() { 
    var bar = $('.logo'); 
    var top = bar.css('top'); 
    $(window).scroll(function() { 
     if($(this).scrollTop() > 700) { 
      bar.stop().animate({'top' : '35px'}, 300); 
     } else { 
      bar.stop().animate({'top' : top}, 300); 
     } 
    }); 
}); 
</script> 

感谢

+0

我们需要更多的信息,而不仅仅是一个链接到您的网页。请看这个:http://meta.stackexchange.com/q/125997 – 2013-03-07 18:15:06

回答

1

event.preventDefault();阻止默认操作,这是哈希附加到URL,然后滚动。

尝试添加:location.hash = this.hash之后event.preventDefault();

+0

这样做了,谢谢! – julesvm 2013-03-07 18:23:46

+0

不客气:) – 2013-03-07 18:24:46

2

尝试把哈希与JS ...

$(".scroll").click(function(event){  
    event.preventDefault(); 
    $('html,body').animate({scrollTop:$(this.hash).offset().top}, 400); 
    window.location.hash = "hash"; 
}); 
1

试试这个,它的工作原理:

$('a[href^="#"]').click(function(event){  
    event.preventDefault(); 
    $('html,body').animate({scrollTop:$(this.hash).offset().top}, 800); 
    var target_anchor = this.hash; 
    setTimeout(function(){ 
     window.location.hash = target_anchor; 
    }, 800); 
});