Skip to content

Commit 1fc7a6e

Browse files
authored
Merge pull request #330 from canjs/276-active-result-sticks-to-bottom
Active search result sticks to bottom and top of page
2 parents ead14df + 2655878 commit 1fc7a6e

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bit-docs-html-canjs",
3-
"version": "1.0.3",
3+
"version": "1.0.3-pre.1",
44
"description": "The plugins to produce the CanJS site",
55
"main": "static/canjs",
66
"scripts": {
@@ -41,7 +41,7 @@
4141
},
4242
"devDependencies": {
4343
"bit-docs-generate-html": "^0.6.0",
44-
"bit-docs-generate-searchmap": "^0.1.1",
44+
"bit-docs-generate-searchmap": "^0.1.2",
4545
"bit-docs-html-toc": "^0.6.0",
4646
"connect": "^3.5.0",
4747
"normalize.css": "^5.0.0",

static/search.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,43 @@ var Search = Control.extend({
674674
// function activateResult
675675
// sets property and adds class to active result
676676
activateResult: function($result){
677+
// Get position top of last active element
678+
var lastResultPosTop = parseInt(this.$activeResult && this.$activeResult.position().top || 0, 10);
677679
this.deactivateResult();
678680
this.$activeResult = $result;
679681
this.$activeResult.addClass(this.options.keyboardActiveClass);
680682

681-
var activeResultOffset = this.getActiveResultOffset();
683+
// Get position top of current active element
684+
var activeResultPosTop = parseInt(this.$activeResult.position().top);
685+
var resultsContainerHeight = this.$resultsContainer.height();
682686

683-
this.$resultsContainer.scrollTop(this.$activeResult.position().top - activeResultOffset);
687+
// Detect if the user is arrowing down
688+
var isMovingDown = lastResultPosTop < this.$activeResult.position().top;
689+
var isBelow = activeResultPosTop > resultsContainerHeight;
690+
var isAbove = activeResultPosTop < 0;
691+
692+
if(isMovingDown && isBelow){
693+
this.resetScrollToBottom(activeResultPosTop, resultsContainerHeight);
694+
}
695+
else if(isAbove){
696+
this.resetScrollToTop(activeResultPosTop);
697+
}
698+
},
699+
700+
resetScrollToTop: function(activeResultPosTop){
701+
this.$resultsContainer.scrollTop(
702+
this.$resultsContainer.scrollTop() +
703+
activeResultPosTop
704+
);
705+
},
706+
707+
resetScrollToBottom: function(activeResultPosTop, resultsContainerHeight){
708+
// Calculate active result's position bottom
709+
var scrollTo = (activeResultPosTop + this.$activeResult.outerHeight()) -
710+
// Calculate the current scrolled position of the bottom of the list
711+
(this.getActiveResultOffset() + resultsContainerHeight);
712+
713+
this.$resultsContainer.scrollTop(scrollTo);
684714
},
685715

686716
// function deactivateResult

0 commit comments

Comments
 (0)