2016-12-01 51 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我意識到這太晚了,看到我的編輯。 –