UX: smoother j/k handling

This commit is contained in:
Sam 2015-06-23 16:38:50 +10:00
parent f35b7c1712
commit afa11b214e
1 changed files with 18 additions and 8 deletions

View File

@ -311,22 +311,32 @@ Discourse.KeyboardShortcuts = Ember.Object.createWithMixins({
tabLoc.focus(); tabLoc.focus();
} }
var rgx = new RegExp("post-cloak-(\\d+)").exec($article.parent()[0].id); this._scrollList($article, direction);
if (rgx === null || typeof rgx[1] === 'undefined') {
this._scrollList($article, direction);
} else {
Discourse.URL.jumpToPost(rgx[1]);
}
} }
}, },
_scrollList: function($article) { _scrollList: function($article) {
// Try to keep the article on screen // Try to keep the article on screen
var scrollPos = $article.position().top - ($(window).height() * 0.5); var pos = $article.offset();
var height = $article.height();
var scrollTop = $(window).scrollTop();
var windowHeight = $(window).height();
// skip if completely on screen
if (pos.top > scrollTop && (pos.top + height) < (scrollTop + windowHeight)) {
return;
}
var scrollPos = (pos.top + (height/2)) - (windowHeight * 0.5);
if (scrollPos < 0) { scrollPos = 0; } if (scrollPos < 0) { scrollPos = 0; }
$('html, body').scrollTop(scrollPos);
if (this._scrollAnimation) {
this._scrollAnimation.stop();
}
this._scrollAnimation = $("html, body").animate({ scrollTop: scrollPos + "px"}, 100);
}, },
_findArticles: function() { _findArticles: function() {
var $topicList = $('.topic-list'), var $topicList = $('.topic-list'),
$topicArea = $('.posts-wrapper'); $topicArea = $('.posts-wrapper');