简体   繁体   English

使用 iframe-resizer 滚动嵌入在 iframe 中的 React 应用程序

[英]Scrolling a React app embedded in an iframe using iframe-resizer

I have a React app embedded in an iframe using iframe-resizer:我有一个使用 iframe-resizer 嵌入到 iframe 中的 React 应用程序:

<iframe id="my-frame" src="http://example.com/my-react-app" scrolling="no"></iframe>
<script>iFrameResize({}, 'my-frame')</script>

This is working well in general.这在一般情况下运作良好。 However when the embedded React app switches from a tall page to a short page, it is possible for the React app to go out of sight completely.但是,当嵌入式 React 应用程序从高页面切换到短页面时,React 应用程序可能会完全消失。 I have to manually scroll to the top of the iframe to see the app again.我必须手动滚动到 iframe 的顶部才能再次看到该应用程序。 Is there a way to do this automatically?有没有办法自动做到这一点? I saw the scrollTo API in the iframe-resizer docs, but it's not clear to me how to use it.我在 iframe-resizer 文档中看到了scrollTo API,但我不清楚如何使用它。 Do I have to change my React app or the page that embeds it?我是否必须更改我的 React 应用程序或嵌入它的页面? How?如何? If possible, I would prefer not to change the React app at all.如果可能,我宁愿完全不更改 React 应用程序。

Hope you are doing well.希望你一切顺利。

Shouldn't the iFrame resize itself to a smaller height when embedded app moves to a smaller page?当嵌入式应用程序移动到较小的页面时,iFrame 不应该将自身调整到较小的高度吗? If that happens, scrollToTop will not be needed in this case.如果发生这种情况,在这种情况下将不需要 scrollToTop。

Provided the embedded app is using iframeResizer.contentWindow.js提供嵌入式应用程序使用 iframeResizer.contentWindow.js

Am I missing something?我错过了什么吗?

I have now written a React interface to iframe-resizer, which might help with things like this I the future.我现在已经为 iframe-resizer 编写了一个 React 接口,这可能对我未来的此类事情有所帮助。

https://github.com/davidjbradshaw/iframe-resizer-react https://github.com/davidjbradshaw/iframe-resizer-react

Same issue here.这里同样的问题。 I am showing a (slides-section of a) react-app through an iframe, which is placed inside a wordpress site.我正在通过 iframe 显示一个(a)react-app 的(幻灯片部分),该 iframe 放置在 wordpress 网站内。 All the resizing worked just fine, but the iframe would be placed out of screen-view when the react page resized after the first interaction within it.所有的调整大小都工作得很好,但是当反应页面在第一次交互后调整大小时,iframe 将被放置在屏幕视图之外。

UsingresizeObserver() solved the issue for me.使用resizeObserver()为我解决了这个问题。

I added some code to the iframe.js file in my wordpress theme, resulting into this:我在 wordpress 主题中的 iframe.js 文件中添加了一些代码,结果如下:

iFrameResize({
    log                     : false, // Enable console logging
  
});

//

let myIframeReady = false
let iframeHeightChanges = 0

document.getElementById('my-iframe').onload = function() {
    myIframeReady = true
}

const resizeObserver = new ResizeObserver(entries => {
  if (!myIframeReady ) {
   return false 
  }
  
  if (iframeHeightChanges === 1) {
    document.getElementById('my-iframe').scrollIntoView()
  }
  
  iframeHeightChanges += 1
})

resizeObserver.observe(document.body)

I hope it helps anyone.我希望它可以帮助任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM