From 2ba897f071c35143130f1464819ec100f444d38f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Wed, 15 Aug 2018 18:21:52 +0800 Subject: [PATCH] fix #1989 Fix problem with scroll not working --- src/pages/layouts/Header.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/pages/layouts/Header.js b/src/pages/layouts/Header.js index 6f3169e6..f529be91 100644 --- a/src/pages/layouts/Header.js +++ b/src/pages/layouts/Header.js @@ -16,11 +16,11 @@ class HeaderView extends PureComponent { }; componentDidMount() { - document.getElementById('root').addEventListener('scroll', this.handScroll); + document.addEventListener('scroll', this.handScroll, { passive: true }); } componentWillUnmount() { - document.getElementById('root').removeEventListener('scroll', this.handScroll); + document.removeEventListener('scroll', this.handScroll); } getHeadWidth = () => { @@ -71,16 +71,22 @@ class HeaderView extends PureComponent { } }; - handScroll = () => { + handScroll = e => { const { autoHideHeader } = this.props; const { visible } = this.state; if (!autoHideHeader) { return; } - const { scrollTop } = document.getElementById('root'); + const scrollTop = document.body.scrollTop + document.documentElement.scrollTop; if (!this.ticking) { - this.ticking = false; requestAnimationFrame(() => { + if (this.oldScrollTop > scrollTop) { + this.setState({ + visible: true, + }); + this.scrollTop = scrollTop; + return; + } if (scrollTop > 400 && visible) { this.setState({ visible: false, @@ -91,9 +97,12 @@ class HeaderView extends PureComponent { visible: true, }); } + this.oldScrollTop = scrollTop; this.ticking = false; + return; }); } + this.ticking = false; }; render() { -- GitLab