From 0c2239cd080a15c79b060435742b8891cd7d29ab Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 17 Jun 2018 01:36:41 +0300 Subject: Fix header --- vnext/src/index.js | 156 ++++++++++++++++++++++++++++++++------------- vnext/src/views/index.html | 6 +- 2 files changed, 116 insertions(+), 46 deletions(-) (limited to 'vnext') diff --git a/vnext/src/index.js b/vnext/src/index.js index 9317c8b6..bf1d78ac 100644 --- a/vnext/src/index.js +++ b/vnext/src/index.js @@ -8,64 +8,127 @@ import Thread from './components/Thread'; import LoginButton from './components/LoginButton'; import Footer from './components/Footer'; +const elClassHidden = 'header--hidden' +const elClassBackground = 'header--background'; + class App extends React.Component { constructor(props) { super(props); this.auth = this.auth.bind(this); this.state = { - visitor: { uid: 0 } + visitor: { uid: 0 }, + headerClassName: '' }; + this.dHeight = 0; + this.wHeight = 0; + this.wScrollCurrent = 0; + this.wScrollBefore = 0; + this.wScrollDiff = 0; + + window.addEventListener('scroll', this.throttle(500, () => { + 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 + this.setState({ + headerClassName: '' + }) + } else if (this.wScrollDiff > 0 && this.state.headerClassName.indexOf(elClassHidden) >=0) { + // scrolled up; element slides in + this.setState({ + headerClassName: elClassBackground + }) + } else if (this.wScrollDiff < 0) { + // scrolled down + if (this.wScrollCurrent + this.wHeight >= this.dHeight && this.state.headerClassName.indexOf(elClassHidden) >=0) { + // scrolled to the very bottom; element slides in + this.setState({ + headerClassName: elClassBackground + }) + } else { + // scrolled down; element slides out + this.setState({ + headerClassName: [elClassHidden, elClassBackground].join(' ') + }) + } + } + this.wScrollBefore = this.wScrollCurrent; + })); this.auth(window.localStorage.hash || '') } + 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); + } + }; + }; render() { return ( - - -
-
-
- {this.state.visitor.uid > 0 ? - - : - - } - +
+
+
+ } /> + } /> + } /> + } /> +
+
- -