aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/scroll_to_top.js
blob: 2a754027a24579638c784e3a633c3a6897d020bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Scroll from './scroll.js';

let fold;
let was_below_the_fold = false;

const back_to_top = function(scrolltop) {
    var is_below_the_fold = scrolltop > fold;
    if (is_below_the_fold !== was_below_the_fold) {
        $('#scroll-to-top').toggleClass('hide', !is_below_the_fold);
        was_below_the_fold = is_below_the_fold;
    }
};

const ScrollToTop = {
    enable() {
        var minScrollHeight = Math.min(
            document.body.scrollHeight, document.documentElement.scrollHeight,
            document.body.offsetHeight, document.documentElement.offsetHeight,
            document.body.clientHeight, document.documentElement.clientHeight
        );
        fold = minScrollHeight - (minScrollHeight / 5); // top of fifth portion!
        Scroll.addHandler('back-to-top', back_to_top);
    },
    disable() {
        Scroll.removeHandler('header');
        $('#scroll-to-top').addClass('hide');
    },
    moveBack() {
        $('#scroll-to-top').on('click', function(e) {
            $('html, body').stop().animate({
                scrollTop: (0)
            }, 1000, 'easeInOutExpo');
            e.preventDefault();
        });
    }
};

export default ScrollToTop;