gitlab-org--gitlab-foss/app/assets/javascripts/lib/utils/scroll_utils.js
Filipa Lacerda bcc9492cbd Extracts scroll position checks into reusable functions
With the new Job Log page in Vue, we'll need to reuse the same functions for scrolling
that we're using in the jQuery one.
This page extracts that logic into reusable functions
2018-09-26 17:35:23 +01:00

53 lines
1.3 KiB
JavaScript

import $ from 'jquery';
export const canScroll = () => $(document).height() > $(window).height();
/**
* Checks if the entire page is scrolled down all the way to the bottom
* @returns {Boolean}
*/
export const isScrolledToBottom = () => {
const $document = $(document);
const currentPosition = $document.scrollTop();
const scrollHeight = $document.height();
const windowHeight = $(window).height();
return scrollHeight - currentPosition === windowHeight;
};
/**
* Checks if page is scrolled to the top
* @returns {Boolean}
*/
export const isScrolledToTop = () => $(document).scrollTop() === 0;
export const scrollDown = () => {
const $document = $(document);
$document.scrollTop($document.height());
};
export const scrollUp = () => {
$(document).scrollTop(0);
};
/**
* Checks if scroll position is in the middle of the page
* @returns {Boolean}
*/
export const isScrolledToMiddle = () => {
const $document = $(document);
const currentPosition = $document.scrollTop();
const scrollHeight = $document.height();
const windowHeight = $(window).height();
return currentPosition > 0 && scrollHeight - currentPosition !== windowHeight;
};
export const toggleDisableButton = ($button, disable) => {
if (disable && $button.prop('disabled')) return;
$button.prop('disabled', disable);
};
export default {};