JQuery滚动条错误

时间:2013-01-30 17:01:32

标签: javascript jquery

我正在使用页面滚动器的这个示例,它也会在滚动时将导航设置为活动状态。

Example

希望你能提供帮助。

在控制台中我看到了这个错误。

  

TypeError:$(...)。offset(...)未定义[打破此错误]

     

contentTop.push($($(this).attr('href'))。offset()。top);

HTML

<nav id="nav">
    <ul>
        <li><a href="#1">B</a></li>
        <li><a href="#2">B</a></li>
        <li><a href="#3">C</a></li>
        <li><a href="#4">D</a></li>
        <li><a href="#5">E</a></li>
        <li><a href="#6">F</a></li>
    </ul>
</nav>

JS

smoothScroll: function() {
     var topRange      = 200,  // measure from the top of the viewport to X pixels down
         edgeMargin    = 20,   // margin above the top or margin from the end of the page
         animationTime = 1200, // time in milliseconds
         contentTop = [];

     // Stop animated scroll if the user does something
     $('html,body').bind('scroll mousedown DOMMouseScroll mousewheel keyup', function(e){
     if ( e.which > 0 || e.type == 'mousedown' || e.type == 'mousewheel' ){
      $('html,body').stop();
     }
    })

     // Set up content an array of locations
     $('#nav').find('a').each(function(){
      contentTop.push( $( $(this).attr('href') ).offset().top );
     })

     // Animate menu scroll to content
      $('#nav').find('a').click(function(){
       var sel = this,
           newTop = Math.min( contentTop[ $('#nav a').index( $(this) ) ], $(document).height() - $(window).height() ); // get content top or top position if at the document bottom
       $('html,body').stop().animate({ 'scrollTop' : newTop }, animationTime, function(){
        window.location.hash = $(sel).attr('href');
       });
       return false;
     })

     // adjust side menu
     $(window).scroll(function(){
      var winTop = $(window).scrollTop(),
          bodyHt = $(document).height(),
          vpHt = $(window).height() + edgeMargin;  // viewport height + margin
      $.each( contentTop, function(i,loc){
       if ( ( loc > winTop - edgeMargin && ( loc < winTop + topRange || ( winTop + vpHt ) >= bodyHt ) ) ){
        $('#nav li a')
         .removeClass('selected')
         .eq(i).addClass('selected');
       }
      })
     })
  },

0 个答案:

没有答案