This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/offset-calculator.js.es6
2018-06-15 17:03:24 +02:00

38 lines
1.1 KiB
JavaScript

export function scrollTopFor(y) {
return y - offsetCalculator();
}
export function minimumOffset() {
const $header = $("header.d-header");
const headerHeight = $header.outerHeight(true) || 0;
const headerPositionTop = $header.position().top;
return headerHeight + headerPositionTop;
}
export default function offsetCalculator() {
const min = minimumOffset();
// on mobile, just use the header
if ($("html").hasClass("mobile-view")) return min;
const $window = $(window);
const windowHeight = $window.height();
const documentHeight = $(document).height();
const topicBottomOffsetTop = $("#topic-bottom").offset().top;
// the footer is bigger than the window, we can scroll down past the last post
if (documentHeight - windowHeight > topicBottomOffsetTop) return min;
const scrollTop = $window.scrollTop();
const visibleBottomHeight = scrollTop + windowHeight - topicBottomOffsetTop;
if (visibleBottomHeight > 0) {
const bottomHeight = documentHeight - topicBottomOffsetTop;
const offset =
((windowHeight - bottomHeight) * visibleBottomHeight) / bottomHeight;
return Math.max(min, offset);
}
return min;
}