Skip to content

Instantly share code, notes, and snippets.

Created August 21, 2014 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2b5f137b771452282d68 to your computer and use it in GitHub Desktop.
Save anonymous/2b5f137b771452282d68 to your computer and use it in GitHub Desktop.
$('body').on('keyup.search', function(e) {
if ($('#search-dropdown').is(':visible')) {
if (e.which == 40 || e.which == 38){
var searchController = Discourse.__container__.lookup("controller:search");
var selectedIndex = searchController.get("selectedIndex");
var $searchContainer = $("#search-dropdown");
var elm = $("#search-dropdown li.selected"); //get the selected element
var elm_position = elm.position();
var elm_bottom = elm_position.top+elm.height();
if ( $searchContainer[0].scrollTop + $("#search-dropdown").height() < elm_bottom + $searchContainer[0].scrollTop ){
$searchContainer[0].scrollTop = elm_bottom - $searchContainer.height()+ $searchContainer[0].scrollTop;
} else if ( elm_position.top < 0){
$searchContainer[0].scrollTop += elm_position.top -15;
}
if ( (typeof selectedIndex !== "undefined") && selectedIndex == 0 && $searchContainer[0].scrollTop > 0){
$("#search-dropdown").animate({scrollTop:0},500,'swing');
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment