From 5e1ed9f6e1920c918b907c76f04e9af024c29990 Mon Sep 17 00:00:00 2001 From: Evgeni Spasov Date: Fri, 26 Jul 2013 18:04:18 +0300 Subject: [PATCH] Fixed: page jumps up/down when using arrow keys When using the up and down arrow keys the page is scrolled repeatedly. This fix checks if autocomplete selection is in browser viewport and scrolls page only needed. --- src/controls.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/controls.js b/src/controls.js index cde682f..ecb2a17 100644 --- a/src/controls.js +++ b/src/controls.js @@ -209,15 +209,36 @@ Autocompleter.Base = Class.create({ }, markPrevious: function() { - if(this.index > 0) this.index--; - else this.index = this.entryCount-1; - this.getEntry(this.index).scrollIntoView(true); + if(this.index > 0) { + this.index--; + } else { + this.index = this.entryCount - 1; + } + + this.scrollIntoView(); }, markNext: function() { - if(this.index < this.entryCount-1) this.index++; - else this.index = 0; - this.getEntry(this.index).scrollIntoView(false); + if(this.index < this.entryCount-1) { + this.index++; + } else { + this.index = 0; + } + + this.scrollIntoView(); + }, + + scrollIntoView: function() { + var selection_top = this.getEntry(this.index).viewportOffset().top; + var document_top = document.viewport.getScrollOffsets().top; + + if(selection_top < document_top) { + this.element.scrollIntoView(true); + } + + else if(selection_top > document_top + document.viewport.getDimensions().height) { + this.getEntry(this.entryCount - 1).scrollIntoView(false); + } }, getEntry: function(index) {