Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 18, 2020
1 parent e362c34 commit c107649
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
4 changes: 4 additions & 0 deletions public/src/ajaxify.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ ajaxify = window.ajaxify || {};
}

ajaxify.end = function (url, tpl_url) {
// Scroll back to top of page
if (!ajaxify.isCold()) {
window.scrollTo(0, 0);
}
ajaxify.loadScript(tpl_url, function done() {
$(window).trigger('action:ajaxify.end', { url: url, tpl_url: tpl_url, title: ajaxify.data.title });
});
Expand Down
5 changes: 0 additions & 5 deletions public/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,6 @@ app.cacheBuster = null;
app.createUserTooltips($('#content'));

app.createStatusTooltips();

// Scroll back to top of page
if (!ajaxify.isCold()) {
window.scrollTo(0, 0);
}
};

app.showMessages = function () {
Expand Down
32 changes: 21 additions & 11 deletions public/src/modules/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co
var scrollTop = $(window).scrollTop();
var windowHeight = $(window).height();
var documentHeight = $(document).height();
var middleOfViewport = scrollTop + (windowHeight / 2);
var navbarHeight = components.get('navbar').outerHeight(true);
var topicHeaderHeight = $('.topic-header').height() || 0;
var middleOfViewport = scrollTop + (windowHeight / 2) - navbarHeight - topicHeaderHeight;
var previousDistance = Number.MAX_VALUE;
els.each(function () {
var elIndex = parseInt($(this).attr('data-index'), 10);
Expand Down Expand Up @@ -387,23 +389,27 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co
return;
}
var postHeight = scrollTo.height();
var viewportHeight = $(window).height();
var navbarHeight = components.get('navbar').height();
var navbarHeight = components.get('navbar').outerHeight(true);
var topicHeaderHeight = $('.topic-header').height() || 0;
var viewportHeight = $(window).height() - navbarHeight - topicHeaderHeight;

// Temporarily disable navigator update on scroll
$(window).off('scroll', navigator.update);
$(window).off('scroll', navigator.delayedUpdate);

duration = duration !== undefined ? duration : 400;
navigator.scrollActive = true;
var done = false;

function animateScroll() {
function reenableScroll() {
// Re-enable onScroll behaviour
$(window).on('scroll', navigator.delayedUpdate);
var scrollToRect = scrollTo.get(0).getBoundingClientRect();
navigator.update(scrollToRect.top);
}
function onAnimateComplete() {
if (done) {
// Re-enable onScroll behaviour
$(window).on('scroll', navigator.update);
var scrollToRect = scrollTo.get(0).getBoundingClientRect();
navigator.update(scrollToRect.top);
reenableScroll();
return;
}
done = true;
Expand All @@ -416,13 +422,16 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co

var scrollTop = 0;
if (postHeight < viewportHeight) {
scrollTop = (scrollTo.offset().top - (viewportHeight / 2) + (postHeight / 2));
scrollTop = (scrollTo.offset().top - (viewportHeight / 2) + (postHeight / 2)) - topicHeaderHeight;
} else {
scrollTop = scrollTo.offset().top - navbarHeight;
scrollTop = scrollTo.offset().top - navbarHeight - topicHeaderHeight;
}

if (duration === 0) {
$(window).scrollTop(scrollTop);
return onAnimateComplete();
onAnimateComplete();
reenableScroll();
return;
}
$('html, body').animate({
scrollTop: scrollTop + 'px',
Expand All @@ -444,3 +453,4 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co

return navigator;
});

0 comments on commit c107649

Please sign in to comment.