2016-12-28 5 views
0

このコードはもともと、ページの読み込み時にスムーズにアンカーにスクロールするために使用されていました。同じ関数のjquery、onclick、およびonloadイベント

リスナーをクリックして同じページのアンカーにスクロールしても動作するようにリスナーを追加しようとしましたが、止まっています。ページのナビゲーションのための

// <!--smooth scroll to anchors--> 
    (function(jQuery){ 
     var jump=function(e){ 
     if (e){ 
      e.preventDefault();     //prevent the "normal" behavior which would be a "hard" jump 
      var mytarget = jQuery(this).attr("href"); //get the target 
     }else{ 
      var mytarget = location.hash;    //sets the target to the anchor part of a URL 
     } 

     jQuery('html,body').animate(       //perform animated scrolling 
     { 
     scrollTop: jQuery(mytarget).offset().top - 100 //get top-position of target-element and set it as scroll target and move down 100px for fixed nav 
     }, 1000,function(){       //scrolldelay: 2 seconds 
     location.hash = mytarget;     //attach the hash (#jumptarget) to the pageurl 
     }); 
     } 

     jQuery('html, body').hide() 

     // on load 
     jQuery(document).ready(function(){ 
     jQuery('a[href^="#"]').bind("click", jump); 

     if (location.hash){ 
      setTimeout(function(){ 
      jQuery('html, body').scrollTop(0).show() 
      jump() 
      }, 0); 
     }else{ 
      jQuery('html, body').show() 
     } 
     }); 

     // on click 
     var inpage-nav =document.querySelectorAll('.in-page-nav'); 
     for(var i=0;i<cell.length;i++){ 
     inpage-nav[i].addEventListener('click',jump); 
     } 

    })(jQuery) 
// <!--End smooth scroll--> 

HTML:

<div class="just-a-nav"> 
     <ul> 
     <li class="filter"><a class="in-page-nav" href="#item1">Item 1</a></li> 
     <li class="filter"><a class="in-page-nav" href="#item2">Item 2</a></li> 
     <li class="filter"><a class="in-page-nav" href="#item3">Item 3</a></li> 
     </ul> 
    </div> 
+0

_Trying LISTENER_を追加するためにここに私のフィドルはありますか? –

+0

あなたのHTMLは何ですか?クラス「ページ内ナビゲーション」を持つHTML部品を表示できますか? –

+0

@RezaMamun元の質問に追加されました。 – justsomeone

答えて

1

サンプルHTML:がうまくいけば、これが役立ちます。 https://jsfiddle.net/Ledg92c7/

<a href="#goto-1" class="in-page-nav">Go-Target-1</a> | <a href="#goto-2" class="in-page-nav">Go-Target-2</a> 
<div id="goto-0" style="height:1200px;background:green"> 
    .....Demo Content 0.... 
</div> 
<div id="goto-1" style="height:300px;background:red"> 
    .....Demo Content 1.... 
</div> 
<div id="goto-2" style="height:400px;background:blue"> 
    .....Demo Content 2.... 
</div> 

のjQuery: - あなたのリスナーがある

(function($){ 
    var jump=function(e){ 
     if (e){ 
      e.preventDefault(); //prevent the "normal" behavior which would be a "hard" jump 
      var mytarget = $(this).attr("href"); //get the target 
     }else{ 
      var mytarget = location.hash; //sets the target to the anchor part of a URL 
     } 

     $('html,body').animate({ //perform animated scrolling 
      scrollTop: $(mytarget).offset().top - 100 //get top-position of target-element and set it as scroll target and move down 100px for fixed nav 
     }, 1000,function(){ //scrolldelay: a few milliseconds 
      location.hash = mytarget; //attach the hash (#jumptarget) to the pageurl 
     }); 
    } 

    $('html, body').hide(); 

    // on load 
    $(document).ready(function(){ 
     $('a[href^="#"]').bind("click", jump); 

     if (location.hash){ 
      setTimeout(function(){ 
       $('html, body').scrollTop(0).show(); 
       jump(); 
      }, 0); 
     }else{ 
      $('html, body').show(); 
     } 
    }); 

    // on click 
    /*var inpageNav =document.querySelectorAll('.in-page-nav'); 
    for(var i=0;i<inpageNav.length;i++){ 
     inpageNav[i].addEventListener('click',jump); 
    }*/ 
    $('.in-page-nav').on('click',jump); 

})(jQuery); 
+0

ありがとうございます。ありがとう! 変更点私は何を間違っていたのですか? – justsomeone

+0

#1。 JS変数名にハイフン( ' - ')を付けるべきではありません。 'var inpage-nav = document.querySelectorAll( '。ページ内ナビ');および#2。 for(var i = 0; i

+0

私は魚を与えてくれて、次回はキャッチするように教えてくれてありがとうございます。 – justsomeone

関連する問題