scroll to comment using parent window
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user