You can do something like this (using JQuery), on a scroll event callback, using a timeout to limit the frequency of the event, plus a cursor of the last found visible element, in way to perform a quicker search on next event. Here 'this' is the context of your view :
onScroll: function(e) {
if (this.scrollEvent) {
return;
}
this.scrollEvent = true;
setTimeout(function(view) {
// @replace you should check here is you view still exists using a status on your view context
if (view.isDestroyed) {
return;
}
view.scrollEvent = false;
var top = $('youselector on tbody').parent().parent().offset().top;
if (view.previousTopElement) {
if (view.previousTopElement.offset().top <= top) {
// the element is the previous, or on bottom of previous
var element = view.previousTopElement;
var i = view.previousTopElementIndex;
view.previousTopElement = null;
while (element.length) {
if (element.offset().top + element.height() >= top) {
// you can uses i + 1 for a 1 based position to display
view.previousTopElement = element;
view.previousTopElementIndex = i;
return;
}
element = element.next();
++i;
}
} else if (view.previousTopElement.offset().top > top) {
// the element is before the previous
var element = view.previousTopElement;
var i = view.previousTopElementIndex;
this.previousTopElement = null;
while (element.length) {
if (element.offset().top <= top) {
// you can uses i + 1 for a 1 based position to display
view.previousTopElement = element;
view.previousTopElementIndex = i;
return;
}
element = element.prev();
--i;
}
}
}
// no previous, get all rows
var rows = $('youselector on tbody').children('tr');
view.previousTopElement = null;
view.previousTopElementIndex = 0;
$.each(rows, function (i, el) {
var element = $(el);
if (element.offset().top + element.height() >= top) {
// you can uses i + 1 for a 1 based position to display
view.previousTopElement = element;
view.previousTopElementIndex = i;
return false;
}
});
}, 100, this); // you can adjust the timeout, pass your view context
}
This is the idea. In my case I do this optimisation because I can have many rows.