I have a bizarre problem, I open a RadWindow with a repeater in ASP.NET, and I want to scroll the repeater items, as the user presses the arrows.
Code:
function isScrolledIntoView(elem) {
var docViewTop;
var docViewBottom;
var elemTop;
var elemBottom;
try {
docViewTop = $(window).scrollTop();
docViewBottom = docViewTop + $(window).height();
elemTop = $(elem).offset().top;
elemBottom = elemTop + $(elem).height();
} catch(err) {
return true;
}
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
function ScrollViewTo(element){
$('#myRepeater').animate({ scrollTop: $(element).height()}, 'fast');
};
Another JS function calls the above (when capturing keypresses):
if (!isScrolledIntoView(selectedEntry)) {
ScrollViewTo(selectedEntry);
}
...where selectedEntry is the next() element that I am going to scroll to.
The logic is that when arrows are used, it checks if the requested (next) element is out of view. If yes, scroll to it. Pretty much like Windows Explorer scrolls if the element in folder is out of the window. The odd thing is, it works only for the first scroll; subsequent keypresses enter the method correctly and call ScrollViewTo, but don't actually scroll. I inspected everything in the debugger and all values show correctly.
I'm quite stumped by this one, having spent quite some time on it - is there something I'm missing? Thanks
I had a d'oh moment and found the bug.
window.alreadyScrolled = 0;
function ScrollViewTo(element) {
window.alreadyScrolled += $(element).height();
$('#myRepeater').animate({ scrollTop: alreadyScrolled}, 'fast');
};
The problem was that the value was always identical with the last time, and it doesn't increment scroll. It was working properly, I just had to read up on scrollTo.