From 6ddc233d32acf53e1def1e7db91a56a9040fdca7 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 8 Oct 2020 17:06:41 -0400 Subject: [PATCH] fix: #8654, use window.location.hash instead of animating w/ jQuery --- public/src/client/topic.js | 6 +-- public/src/client/topic/posts.js | 2 +- public/src/modules/navigator.js | 76 +++++++++----------------------- 3 files changed, 22 insertions(+), 62 deletions(-) diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 9b0e0d0621..c3f96b1c5e 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -126,7 +126,7 @@ define('forum/topic', [ if (postIndex > 1) { if (components.get('post/anchor', postIndex - 1).length) { - return navigator.scrollToPostIndex(postIndex - 1, true, 0); + return navigator.scrollToPostIndex(postIndex - 1, true); } } else if (bookmark && (!config.usePagination || (config.usePagination && ajaxify.data.pagination.currentPage === 1)) && ajaxify.data.postcount > ajaxify.data.bookmarkThreshold) { app.alert({ @@ -208,10 +208,6 @@ define('forum/topic', [ return; } - if (navigator.scrollActive) { - return; - } - var newUrl = 'topic/' + ajaxify.data.slug + (index > 1 ? ('/' + index) : ''); if (newUrl !== currentUrl) { diff --git a/public/src/client/topic/posts.js b/public/src/client/topic/posts.js index b848d403ff..bcaa112ecd 100644 --- a/public/src/client/topic/posts.js +++ b/public/src/client/topic/posts.js @@ -223,7 +223,7 @@ define('forum/topic/posts', [ } Posts.loadMorePosts = function (direction) { - if (!components.get('topic').length || navigator.scrollActive) { + if (!components.get('topic').length) { return; } diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js index 7655e4e6f4..9cafc37ebc 100644 --- a/public/src/modules/navigator.js +++ b/public/src/modules/navigator.js @@ -14,8 +14,6 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co var isNavigating = false; var firstMove = true; - navigator.scrollActive = false; - var paginationBlockEl = $('.pagination-block'); var paginationTextEl = paginationBlockEl.find('.pagination-text'); var paginationBlockMeterEl = paginationBlockEl.find('meter'); @@ -107,7 +105,7 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co if (isNavigating) { touchTooltipEl.addClass('hidden'); - navigator.scrollToIndex(index - 1, true, 0); + navigator.scrollToIndex(index - 1, true); isNavigating = false; } }); @@ -331,7 +329,7 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co } }; - navigator.scrollToIndex = function (index, highlight, duration) { + navigator.scrollToIndex = function (index, highlight) { var inTopic = !!components.get('topic').length; var inCategory = !!components.get('category').length; @@ -339,21 +337,17 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co return; } - duration = duration !== undefined ? duration : 400; - navigator.scrollActive = true; - // if in topic and item already on page if (inTopic && components.get('post/anchor', index).length) { - return navigator.scrollToPostIndex(index, highlight, duration); + return navigator.scrollToPostIndex(index, highlight); } // if in category and item alreay on page if (inCategory && $('[component="category/topic"][data-index="' + index + '"]').length) { - return navigator.scrollToTopicIndex(index, highlight, duration); + return navigator.scrollToTopicIndex(index, highlight); } if (!config.usePagination) { - navigator.scrollActive = false; index = parseInt(index, 10) + 1; ajaxify.go(generateUrl(index)); return; @@ -364,70 +358,42 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co var page = 1 + Math.floor(index / config.postsPerPage); if (parseInt(page, 10) !== ajaxify.data.pagination.currentPage) { pagination.loadPage(page, function () { - scrollMethod(index, highlight, duration); + scrollMethod(index, highlight); }); } else { - scrollMethod(index, highlight, duration); + scrollMethod(index, highlight); } }; - navigator.scrollToPostIndex = function (postIndex, highlight, duration) { + navigator.scrollToPostIndex = function (postIndex, highlight) { var scrollTo = components.get('post', 'index', postIndex); - navigator.scrollToElement(scrollTo, highlight, duration); + navigator.scrollToElement(scrollTo, highlight); }; - navigator.scrollToTopicIndex = function (topicIndex, highlight, duration) { + navigator.scrollToTopicIndex = function (topicIndex, highlight) { var scrollTo = $('[component="category/topic"][data-index="' + topicIndex + '"]'); - navigator.scrollToElement(scrollTo, highlight, duration); + navigator.scrollToElement(scrollTo, highlight); }; - navigator.scrollToElement = function (scrollTo, highlight, duration) { + navigator.scrollToElement = function (scrollTo, highlight) { if (!scrollTo.length) { - navigator.scrollActive = false; return; } - var postHeight = scrollTo.height(); - var viewportHeight = $(window).height(); - var navbarHeight = components.get('navbar').height(); // Temporarily disable navigator update on scroll $(window).off('scroll', navigator.update); - duration = duration !== undefined ? duration : 400; - navigator.scrollActive = true; - var done = false; + const anchorId = scrollTo.find('[component="post/anchor"]').prop('id'); + window.location.hash = anchorId; - function animateScroll() { - function onAnimateComplete() { - if (done) { - // Re-enable onScroll behaviour - $(window).on('scroll', navigator.update); - var scrollToRect = scrollTo.get(0).getBoundingClientRect(); - navigator.update(scrollToRect.top); - return; - } - done = true; + highlightPost(); + $('body').scrollTop($('body').scrollTop() - 1); + $('html').scrollTop($('html').scrollTop() - 1); - navigator.scrollActive = false; - highlightPost(); - $('body').scrollTop($('body').scrollTop() - 1); - $('html').scrollTop($('html').scrollTop() - 1); - } - - var scrollTop = 0; - if (postHeight < viewportHeight) { - scrollTop = (scrollTo.offset().top - (viewportHeight / 2) + (postHeight / 2)); - } else { - scrollTop = scrollTo.offset().top - navbarHeight; - } - if (duration === 0) { - $(window).scrollTop(scrollTop); - return onAnimateComplete(); - } - $('html, body').animate({ - scrollTop: scrollTop + 'px', - }, duration, onAnimateComplete); - } + // Re-enable onScroll behaviour + $(window).on('scroll', navigator.update); + var scrollToRect = scrollTo.get(0).getBoundingClientRect(); + navigator.update(scrollToRect.top); function highlightPost() { if (highlight) { @@ -438,8 +404,6 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co }, 10000); } } - - animateScroll(); }; return navigator;