UX: Only scroll if the element is not in viewport.

This commit is contained in:
Guo Xiang Tan 2016-11-10 09:38:35 +08:00
parent eaf7746ec9
commit 55865024c7
1 changed files with 3 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import DiscourseURL from 'discourse/lib/url';
import { categoryBadgeHTML } from 'discourse/helpers/category-link'; import { categoryBadgeHTML } from 'discourse/helpers/category-link';
import Post from 'discourse/models/post'; import Post from 'discourse/models/post';
import debounce from 'discourse/lib/debounce'; import debounce from 'discourse/lib/debounce';
import isElementInViewport from "discourse/lib/is-element-in-viewport";
export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, { export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
composer: Ember.inject.controller(), composer: Ember.inject.controller(),
@ -864,9 +865,9 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
_scrollToPost: debounce(function(postNumber) { _scrollToPost: debounce(function(postNumber) {
const $post = $(`.topic-post article#post_${postNumber}`); const $post = $(`.topic-post article#post_${postNumber}`);
if ($post.length === 0) return; if ($post.length === 0 || isElementInViewport($post)) return;
$('body').animate({ scrollTop: $post.offset().top }, 1000); $('body').animate({ scrollTop: $post.offset().top - $post.height() }, 1000);
}, 500), }, 500),
unsubscribe() { unsubscribe() {