2016-12-01 54 views
0

在下面的示例代码中,使用回调函数作为scrollTop()的参数。jQuery的scrollTop()接受回调函数吗?

http://jsfiddle.net/xjrLN/2/

什么是参数值i和回调诉?

jQuery(document).ready(function() { 
    var is_dragging = false; 

    $('ul').disableSelection().sortable({ 
     axis: 'y', 
     start: function() { is_dragging = true }, 
     stop: function() { is_dragging = false } 
    }).mousemove(function(e) { 
     if(is_dragging) { 
      // **** What are parameters' values i and v of the callback? ***** 
      $('ul').scrollTop(function(i, v) { 
       var h = $('ul').height(); 
       var y = e.clientY - h/2; 
       return v + y * 0.1; 
      }); 
     } 
    }); 
}); 

的示例代码是从后 Scrolling a sortable/dragable item's parent container when its border is reached

感谢

回答

1

首先,这不是一个回调函数。在设置一个值以确保同步执行(例如在animate())中后,scrollTop()的回调将触发。而不是设置一个值,例如$('ul').scollTop('20');函数用于计算一个值,然后返回。

经调查,并与jQuery的论坛反馈,它已经很清楚,在设定的jQuery选择当前元素的i回报的指标,v返回scrollTop()该元件的电流值。

$(window).scroll(function() { 
 
    $('body').scrollTop(function(i, v) { 
 
    console.log(i + ' ' + v); 
 
    // i == index of current element in the selector 
 
    // (zero-based of course) 
 
    // (v == $('body').scrollTop()) 
 
    }); 
 
});
body { 
 
    height: 600vh; 
 
    width: 600vw; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

但功能真的叫,请看到我更新的代码中,V和I是输出到控制台。 http://jsfiddle.net/xjrLN/131/ – thermostat

+0

@thermostat我意识到这太晚了,看到我的编辑。 –