scroll to comment using parent window

This commit is contained in:
igoradamenko
2018-05-26 19:51:13 +03:00
parent 0d95254fef
commit faf5ecf598
2 changed files with 11 additions and 2 deletions
+1 -2
View File
@@ -80,8 +80,7 @@ export default class Root extends Component {
if (comment) {
setTimeout(() => {
comment.scrollIntoView();
window.scrollTo(window.scrollX, 0); // sometimes smth goes wrong and iframe scrolls; we return it back
window.parent.postMessage(JSON.stringify({ scrollTo: comment.getBoundingClientRect().top }), '*');
}, 500);
}
}
+10
View File
@@ -61,6 +61,16 @@ function init() {
if (data.remarkIframeHeight) {
iframe.style.height = `${data.remarkIframeHeight}px`;
}
if (data.scrollTo) {
const iframeTop = iframe.getBoundingClientRect().top;
// sometimes browser is already scrolled to the element (like Safari on macOS)
// so let's check it and prevent our scrolling if it so
if (data.scrollTo + iframeTop === 0) return;
window.scrollTo(window.pageXOffset, data.scrollTo + iframe.getBoundingClientRect().top);
}
} catch (e) {}
}