Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在GatsbyJS中将iframe height设置为scrollHeight_Javascript_Css_Reactjs_Iframe_Gatsby - Fatal编程技术网

Javascript 在GatsbyJS中将iframe height设置为scrollHeight

Javascript 在GatsbyJS中将iframe height设置为scrollHeight,javascript,css,reactjs,iframe,gatsby,Javascript,Css,Reactjs,Iframe,Gatsby,移动高度不够大。另外,在桌面模式下,我仍然不明白为什么我的iframe有滚动条,即使包含div没有任何限制???它为什么不把整页的内容都展开呢 我的错误是: TypeError: Cannot read property 'iFrameHeight' of null 我还尝试了下面列出的无反应方式: https://stackoverflow.com/questions/42042901/setting-iframe-height-to-scrollheight-in-reactjs 但它不

移动高度不够大。另外,在桌面模式下,我仍然不明白为什么我的iframe有滚动条,即使包含div没有任何限制???它为什么不把整页的内容都展开呢

我的错误是:

TypeError: Cannot read property 'iFrameHeight' of null
我还尝试了下面列出的无反应方式:

https://stackoverflow.com/questions/42042901/setting-iframe-height-to-scrollheight-in-reactjs
但它不起作用

以下是我的一些代码:

导出默认类SecondPage扩展组件{
componentDidMount(){
const obj=ReactDOM.findDOMNode(this);
this.setState({iFrameHeight:obj.contentWindow.document.body.scrollHeight+'px'});
}
render(){
返回(

); } }
我的css:

.iframe容器{
溢出:隐藏;
垫面:56.25%;
位置:相对位置;
}
.iframe容器iframe{
边界:0;
身高:100%;
左:0;
位置:绝对位置;
排名:0;
宽度:100%;
}

您收到的错误是由于未初始化
状态造成的。您需要在组件中添加如下内容:

constructor(props) {
  super(props)
  this.state = { iFrameHeight: 0 }
}

关于iframe大小的第二个问题:iframe不会根据其内容动态地增长/收缩。有一些方法可以使用
postMessage
API与嵌入式iframe进行通信,但是嵌入式站点需要测量其内容的宽度,并将其提供给父/嵌入站点进行应用

@Link这回答了你的问题吗?