2012-03-17 6 views
0

私はこのスクリプトを持って...jQueryのスムーズなスクロールとURLフィールド

$(function(){ 
$('a[href*=#]:not([href=#])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'#') 
     || location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
      if (target.length) { 
      $('html,body').animate({ scrollTop: target.offset().top }, 1000); 
      location.hash = (this.hash); 
      return false; 
     } 
    } 
}); 
}); 

...しかし、私はそれがスクロールしたときにトップにページジャンプに問題があるが、私はラインを取る場合.. 。

location.hash = (this.hash); 

それは正常に動作しますし、スクロールがすべて良いですが、URLがクリックされたアンカーリンクに追加されません。

基本的に私は表示するURLが必要です:http://www.example.com/#home < - ハッシュに注意してください。

ありがとうございます!

+0

のコールバックを使用してください。 – Kia

答えて

0

は、私はそれがとても他のスクリプトと一緒に行った、作業やったことがなかった.animate

$(function(){ 
$('a[href*=#]:not([href=#])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'#') 
     || location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
      if (target.length) { 
      var hash = this.hash; // 'this' will not be in scope in callback 
      $('html,body').animate({ scrollTop: target.offset().top }, 1000, function() { 
       location.hash = hash; 
      }); 
      return false; 
     } 
    } 
}); 
}); 

関連する問題