#139: preserve scrollTop, after open-close popup

This commit is contained in:
SleepWalker 2016-06-04 10:39:24 +03:00
parent f22ae1ba54
commit a9b0c6804b
5 changed files with 19 additions and 6 deletions

View File

@ -8,9 +8,9 @@ const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestA
|| window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
const TIME_CONSTANT = 100; const TIME_CONSTANT = 100;
export default function scrollTo(y) { export default function scrollTo(y, viewPort) {
const start = Date.now(); const start = Date.now();
const {scrollTop} = document.body; const {scrollTop} = viewPort;
const amplitude = y - scrollTop; const amplitude = y - scrollTop;
requestAnimationFrame(function animateScroll() { requestAnimationFrame(function animateScroll() {
@ -24,7 +24,7 @@ export default function scrollTo(y) {
delta = 0; delta = 0;
} }
window.scrollTo(0, y + delta); viewPort.scrollTop = y + delta;
}); });
} }

View File

@ -65,16 +65,21 @@ function restoreScroll() {
setTimeout(() => { setTimeout(() => {
const id = hash.replace('#', ''); const id = hash.replace('#', '');
const el = id ? document.getElementById(id) : null; const el = id ? document.getElementById(id) : null;
const viewPort = document.getElementById('view-port');
if (!viewPort) {
throw new Error('Can not find viewPort element');
}
let y = 0; let y = 0;
if (el) { if (el) {
const {scrollTop} = document.body; const {scrollTop} = viewPort;
const {top} = el.getBoundingClientRect(); const {top} = el.getBoundingClientRect();
y = scrollTop + top - SCROLL_ANCHOR_OFFSET; y = scrollTop + top - SCROLL_ANCHOR_OFFSET;
} }
scrollTo(y); scrollTo(y, viewPort);
}, 100); }, 100);
} }

View File

@ -14,6 +14,7 @@ body {
background: $light; background: $light;
color: #444; color: #444;
font-size: 16px; font-size: 16px;
overflow: hidden;
} }
b { b {

View File

@ -14,7 +14,7 @@ import messages from './RootPage.intl.json';
function RootPage(props) { function RootPage(props) {
return ( return (
<div className={styles.root}> <div className={styles.root}>
<div className={classNames(styles.root, { <div id="view-port" className={classNames(styles.viewPort, {
[styles.isPopupActive]: props.isPopupActive [styles.isPopupActive]: props.isPopupActive
})}> })}>
<div className={styles.header}> <div className={styles.header}>

View File

@ -5,6 +5,13 @@ $userBarHeight: 50px;
.root { .root {
height: 100%; height: 100%;
overflow: hidden;
}
.viewPort {
height: 100%;
overflow-y: auto;
overflow-x: hidden;
} }
.isPopupActive { .isPopupActive {