blob: 91fa02fd34e40a7bd1ebceaad0b9179309e7af26 (
plain) (
blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import React from 'react';
import PropTypes from 'prop-types';
const elClassHidden = 'header--hidden';
const elClassBackground = 'header--background';
export default class Header extends React.Component {
constructor(props) {
super(props);
this.dHeight = 0;
this.wHeight = 0;
this.wScrollCurrent = 0;
this.wScrollBefore = 0;
this.wScrollDiff = 0;
this.header = React.createRef();
}
componentDidMount() {
window.addEventListener('scroll', () => (!window.requestAnimationFrame)
? this.throttle(250, this.updateHeader)
: window.requestAnimationFrame(this.updateHeader), false);
}
throttle(delay, fn) {
var last, deferTimer;
return function () {
var context = this, args = arguments, now = +new Date;
if (last && now < last + delay) {
clearTimeout(deferTimer);
deferTimer = setTimeout(
function () {
last = now;
fn.apply(context, args);
},
delay);
} else {
last = now;
fn.apply(context, args);
}
};
}
updateHeader = () => {
const header = this.header.current;
this.dHeight = document.body.offsetHeight;
this.wHeight = window.innerHeight;
this.wScrollCurrent = window.pageYOffset;
this.wScrollDiff = this.wScrollBefore - this.wScrollCurrent;
if (this.wScrollCurrent <= 0) {
// scrolled to the very top; element sticks to the top
header.classList.remove(elClassHidden);
header.classList.remove(elClassBackground);
} else if (this.wScrollDiff > 0 && header.classList.contains(elClassHidden)) {
// scrolled up; element slides in
header.classList.remove(elClassHidden);
header.classList.add(elClassBackground);
} else if (this.wScrollDiff < 0) {
// scrolled down
if (this.wScrollCurrent + this.wHeight >= this.dHeight && header.classList.contains(elClassHidden)) {
// scrolled to the very bottom; element slides in
header.classList.remove(elClassHidden);
header.classList.add(elClassBackground);
} else {
// scrolled down; element slides out
header.classList.add(elClassHidden);
}
}
this.wScrollBefore = this.wScrollCurrent;
}
render() {
return (<header id="header" ref={this.header} style={this.props.style || {}}>{this.props.children}</header>);
}
}
Header.propTypes = {
children: PropTypes.node,
style: PropTypes.object
};
|