2017-06-24 7 views
-1

誰も私に次のコードを説明することはできますか?私は構文ではなく論理を意味します。クリックイベントでdivへのスクロールを達成したいのですが、これがどのように行われるのか完全に理解していません。これは本当に悪いのコード例である - - だから、で開始するdivへのスクロールはどのように機能しますか?

$('a[href*="#"]') 
 
    // Remove links that don't actually link to anything 
 
     .not('[href="#"]') 
 
     .not('[href="#0"]') 
 
     .click(function(event) { 
 
     // On-page links 
 
     if (
 
      location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') 
 
      && 
 
      location.hostname == this.hostname 
 
     ) { 
 
      // Figure out element to scroll to 
 
      var target = $(this.hash); 
 
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); 
 
      // Does a scroll target exist? 
 
      if (target.length) { 
 
      // Only prevent default if animation is actually gonna happen 
 
      event.preventDefault(); 
 
      $('html, body').animate({ 
 
       scrollTop: target.offset().top 
 
      }, 1000, function() { 
 
       // Callback after animation 
 
       // Must change focus! 
 
       var $target = $(target); 
 
       $target.focus(); 
 
       if ($target.is(":focus")) { // Checking if the target was focused 
 
       return false; 
 
       } else { 
 
       $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable 
 
       $target.focus(); // Set focus again 
 
       }; 
 
      }); 
 
      } 
 
     } 
 
     });

+0

もっと具体的に説明したいことがありますか? – Icepickle

答えて

-1

なしクラス、無セレクタと、コードはかなり曖昧である;)

スクロールに重要です何

、しかし、このラインである:ここで何が起こっている

$('html, body').animate({ 
    scrollTop: target.offset().top 
}, 1000, function() (...) 

- メインhtmlとエンティティがアニメーションをトリガする - スクロール領域の上端をイベントターゲットであるtargetの上端に移動します。 offset()は、ターゲットの座標を返す関数です。

URL全体に特殊なハッシュがあり、スクロールウインドウの一番上になるべき要素を指し示し、DOM($(this.hash))の要素を検索し、その座標を取得するスクロール自体のためにJQueryアニメーションを実行します。

+0

もちろん、コード全体を「何もない」と置き換えることができ、まったく同じ機能を提供します... –

+0

'$(this.hash)'は*「座標」とは関係ありません* – charlietfl

+0

'$ .hash) 'は' offset() 'を取得するために使用されたターゲットになり、それをスクロールするために使用されます。したがって、' $(this.hash) 'にある要素がmainスクロールエリアに表示されます。 – SzybkiSasza

関連する問題