$(document).ready(function(){
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 1000, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = none;
});
} // End if
});
});
私はこれを実行して、url-hashを変更せずにスムーススクロールして "#top"にします。私は誤ってURLを更新しないように変更する方法を発見しました( "window.location.hash"の "none"を追加しました)が、問題が1つあります。これで関数は繰り返されません。ここで スムーススクロールが繰り返されない上部アンカー
は何が起こるかです:
- ロードページ
- スクロールダウン
- 押してトップアンカー (URLを更新せず)上へ
- ページをスクロール
- 下にスクロールして再び
- プレストップアンカー
- 何も起こらない
私はこのスニペットがどこにあるのかわかりませんが、しばらく私のjs-フォルダに入れました。
'window.location.hash =なし;' - あなたはで見ればあなたのブラウザーコンソール、あなたは "なし"は定義されていない取得している。 'top'リンクをクリックすると、URLに空のハッシュが残らないようにするには、 'body =' top ''または最初/先頭の要素などに 'id =" top "を追加し、そのリンクを' top ' –