动画jQuery无法按预期工作

时间:2017-11-03 13:35:22

标签: javascript jquery

我在网页上有一个FB Feed

enter image description here

当我按下PrevNext按钮时,会显示下一个和上一个新闻Feed。但是我想在滚动时使用animate函数。 我试过使用这行代码:

 $('html, body').stop().animate({ 
   scrollTop: $scrollItems.eq(currentFbDiv).offset().top
 }, 200);

但它没有按预期工作。 能否指导我如何在我的例子中使用?感谢

<script>
jQuery(document).ready(function($){
  var pagePositon = 0,
    sectionsSeclector = $(".cff-item"),
    $scrollItems = $(sectionsSeclector),
    offsetTolorence = 20,
    fbDivHeights = [],
    result = [],
    currentFbDiv = 0,
    $fbDivWrapper = $('#cff'),
    pageMaxPosition = $scrollItems.length-1;
   
  sectionsSeclector.each(function(index, item) {    
    var sum = fbDivHeights.reduce(function(a, b) { return a + b; }, 0);
    fbDivHeights.push( $(item).outerHeight()); 
    result = fbDivHeights.map((s => a => s += a)(0));      
  });   
    
  //Map the sections:
  $scrollItems.each(function(index,ele) { 
    $(ele).attr("debog",index).data("pos",index); 
  });

  // Bind to scroll
  $(window).bind('scroll',upPos);

  //Move on click:
  $('#facebook_ticker_controls a').click(function(e){
    //alert("aaa");
    if ($(this).hasClass('next') && currentFbDiv < pageMaxPosition) {
      currentFbDiv++;           
      $fbDivWrapper.css('margin-top', -result[currentFbDiv-1]); 

      /* pagePositon++;*/
      $('html, body').stop().animate({ 
        scrollTop: $scrollItems.eq(currentFbDiv).offset().top
      }, 200);
    }
    if ($(this).hasClass('prev') && currentFbDiv!= 0 && currentFbDiv <= pageMaxPosition) {
           
      if(currentFbDiv == 1) {
        $fbDivWrapper.css('margin-top', '0px');
      } else {
        currentFbDiv--;
        $fbDivWrapper.css('margin-top', -result[currentFbDiv-1]);
      }
            
      /*$('html, body').stop().animate({ 
        scrollTop: $scrollItems.eq(pagePositon).offset().top
      }, 300);
      return false;*/
    }
    return false;
  });

  //Update position func:
  function upPos(){
    var fromTop = $(this).scrollTop();
    var $cur = null;
    $scrollItems.each(function(index,ele){
      if ($(ele).offset().top < fromTop + offsetTolorence) 
        $cur = $(ele);
    });
    if ($cur != null && pagePositon != $cur.data('pos')) {
      pagePositon = $cur.data('pos');
    }                   
  }
}); 

</script>
<div class="tab-content">
  <div class="tab-pane active fade in">
    <ul class="list-unstyled" id="facebook_ticker">
      <?php echo do_shortcode("[custom-facebook-feed]"); ?> 
    </ul>
  </div>
  <div id="facebook_ticker_controls" class="ticker-controls">
    <div style="position: absolute; z-index: 1000; background-color: rgba(0,0,0,.7); height: 35px; width: 350px;">
      <a href="#" class="prev pull-left" id="fprev"><span class="glyphicon glyphicon-chevron-down"></span>&nbsp; Previous</a> 
      <a href="#" class="next pull-right" id="fnext">Next &nbsp;<span class="glyphicon glyphicon-chevron-up"></span></a> 
    </div>
  </div>
</div><!-- end tab-content -->

0 个答案:

没有答案