Add auto page scrolling to selected result

This commit is contained in:
Kirill Isakov 2016-04-24 21:04:53 +06:00
parent 0d6625e070
commit 7b48a66350
1 changed files with 45 additions and 4 deletions

View File

@ -140,8 +140,21 @@ $(document).ready(function() {
next = which; next = which;
} else { } else {
switch (which) { switch (which) {
// case 'visible': case 'visible':
// TODO var top = $(window).scrollTop();
var bot = top + $(window).height();
var results = $('.result');
for (var i = 0; i < results.length; i++) {
next = $(results[i]);
var etop = next.offset().top;
var ebot = etop + next.height();
if ((ebot <= bot) && (etop > top)) {
break;
}
}
break;
case 'down': case 'down':
next = current.next('.result'); next = current.next('.result');
if (next.length === 0) { if (next.length === 0) {
@ -163,8 +176,11 @@ $(document).ready(function() {
} }
} }
current.removeAttr('data-vim-selected').removeClass('well well-sm'); if (next) {
next.attr('data-vim-selected', 'true').addClass('well well-sm'); current.removeAttr('data-vim-selected').removeClass('well well-sm');
next.attr('data-vim-selected', 'true').addClass('well well-sm');
scrollPageToSelected();
}
} }
} }
@ -193,6 +209,31 @@ $(document).ready(function() {
} }
} }
function scrollPageToSelected() {
var sel = $('.result[data-vim-selected]');
if (sel.length !== 1) {
return;
}
var wnd = $(window);
var wtop = wnd.scrollTop();
var etop = sel.offset().top;
var offset = 30;
if (wtop > etop) {
wnd.scrollTop(etop - offset);
} else {
var ebot = etop + sel.height();
var wbot = wtop + wnd.height();
if (wbot < ebot) {
wnd.scrollTop(ebot - wnd.height() + offset);
}
}
}
function scrollPage(amount) { function scrollPage(amount) {
return function() { return function() {
window.scrollBy(0, amount); window.scrollBy(0, amount);