feat(ui-toolkit): skip parallax header after user scrolls

This commit is contained in:
Fábio Moreira 2018-05-14 12:18:59 +01:00 committed by Sérgio Ramos
parent 77fd895b6c
commit 882085a170

View File

@ -53,12 +53,17 @@ const Img = styled.img`
const gutterWidth = 47 + 70; const gutterWidth = 47 + 70;
const windowWidth = (window || {}).outerWidth + 70; const windowWidth = (window || {}).outerWidth + 70;
let isScrolling;
const isScrolledOutsideView = () => { const isScrolledOutsideView = () => {
const elem = document.getElementById('parallaxWrapper'); const elem = document.getElementById('parallaxWrapper');
const eleHeight = elem.clientHeight; if (elem) {
const scrollY = window.scrollY; const eleHeight = elem.clientHeight;
const scrollY = window.scrollY;
return scrollY > eleHeight; return scrollY >= eleHeight;
}
return true;
}; };
export default class extends Component { export default class extends Component {
@ -68,6 +73,35 @@ export default class extends Component {
show: true show: true
}; };
componentDidMount() {
if (this.props.hideAfterScroll) {
isScrolledOutsideView()
? this.setState({ show: false })
: window.addEventListener('scroll', this.scrollStopHandler);
}
}
scrollStopHandler = () => {
window.clearTimeout(isScrolling);
isScrolling = setTimeout(() => {
isScrolledOutsideView()
? this.setState({ show: false }, () =>
window.removeEventListener('scroll', this.scrollStopHandler)
)
: this.skipParallax();
}, 66);
};
skipParallax = () => {
if (this.state.show) {
window.scroll({
top: document.getElementsByTagName('header')[0].offsetTop,
left: 0,
behavior: 'smooth'
});
}
};
render() { render() {
window.addEventListener('resize', () => { window.addEventListener('resize', () => {
this.setState({ this.setState({
@ -76,14 +110,6 @@ export default class extends Component {
}); });
}); });
if (this.props.hideAfterScroll) {
window.addEventListener('scroll', () => {
if (this.state.show && isScrolledOutsideView()) {
this.setState({ show: false });
}
});
}
const array = Array.apply(null, { length: this.state.value }).map( const array = Array.apply(null, { length: this.state.value }).map(
Number.call, Number.call,
Number Number