Pet*_* G. 7 javascript css iframe reactive-programming reactjs
脚本:
<script>
function resizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>
Run Code Online (Sandbox Code Playgroud)
HTML:
<iframe src="..." frameborder="0" scrolling="no" onload="resizeIframe(this)" />
Run Code Online (Sandbox Code Playgroud)
有一个NPM包react-iframe
,但看起来没有完成(只接受道具url
,width
,height
):
解决方案的可能部分是听取load
事件iframe
,但是以与React兼容的方式.
在React中有一种方法可以将a的高度设置iframe
为其可滚动内容的高度吗?
我的代码:
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import Iframe from 'react-iframe'
export default class FullheightIframe extends Component {
componentDidMount() {
console.log("IFRAME DID MOUNT");
}
renderReactFrame() {
return (
<Iframe url="http://www.example.com" width="100%" height="100%" onLoad={()=>{console.log("IFRAME ON LOAD")}}></Iframe>
);
}
renderHTMLFrame() {
return (
<iframe
onLoad={(loadEvent)=>{
// NOT WORKING var frameBody = ReactDOM.findDOMNode(this).contentDocument.body; // contentDocument undefined
// NOT WORKING obj.nativeEvent.contentWindow.document.body.scrollHeight // contentWindow undefined
}}
ref="iframe"
src="http://www.example.com"
width="100%"
height="100%"
scrolling="no"
frameBorder="0"
/>
);
}
render() {
return (
<div style={{maxWidth:640, width:'100%', height:'100%', overflow:'auto'}}>
{this.renderHTMLFrame()}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是答案,但前两个重要的事情.
render()
方法onLoad
事件中捕获高度(如果满载,则为iframe)这是完整的代码:
import React, { Component, PropTypes } from 'react'
import ReactDOM from 'react-dom'
export default class FullheightIframe extends Component {
constructor() {
super();
this.state = {
iFrameHeight: '0px'
}
}
render() {
return (
<iframe
style={{maxWidth:640, width:'100%', height:this.state.iFrameHeight, overflow:'visible'}}
onLoad={() => {
const obj = ReactDOM.findDOMNode(this);
this.setState({
"iFrameHeight": obj.contentWindow.document.body.scrollHeight + 'px'
});
}}
ref="iframe"
src="http://www.example.com"
width="100%"
height={this.state.iFrameHeight}
scrolling="no"
frameBorder="0"
/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
这里有几点需要注意:
componentDidMount()
您冒着内容不存在的风险一样。class WrappedFrame extends React.Component {
state = { contentHeight: 100 };
handleResize = () => {
const { body, documentElement } = this.container.contentWindow.document;
const contentHeight = Math.max(
body.clientHeight,
body.offsetHeight,
body.scrollHeight,
documentElement.clientHeight,
documentElement.offsetHeight,
documentElement.scrollHeight
);
if (contentHeight !== this.state.contentHeight) this.setState({ contentHeight });
};
onLoad = () => {
this.container.contentWindow.addEventListener('resize', this.handleResize);
this.handleResize();
}
componentWillUnmount() {
this.container.contentWindow.removeEventListener('resize', this.handleResize);
}
render() {
const { contentHeight } = this.state;
return (
<iframe
frameBorder="0"
onLoad={this.onLoad}
ref={(container) => { this.container = container; }}
scrolling="no"
src="your.source"
style={{ width: '100%', height: `${contentHeight}px` }}
title="Some Content"
/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我们将确定的内容高度存储在组件的状态中,并使用该状态来设置呈现的 iframe 的高度。此外,通过将onLoad()
处理程序定义放在组件中,您render()
无需在每次重新渲染时创建新的处理程序函数,从而节省一点性能。
你想要做的是在你的 componentDidMount 中,运行脚本来设置高度。[如果您正在加载外部内容,您可能需要在 IFrame 上添加事件侦听器以等待外部内容加载完毕。]
componentDidMount() {
const obj = ReactDOM.findDOMNode(this);
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
Run Code Online (Sandbox Code Playgroud)
还有另一种更“反应”的方式来做到这一点 - 您可以将高度存储在状态中。
componentDidMount() {
const obj = ReactDOM.findDOMNode(this);
this.setState({iFrameHeight: obj.contentWindow.document.body.scrollHeight + 'px'});
}
Run Code Online (Sandbox Code Playgroud)
然后在你的渲染中:
render() {
return (
<div style={{maxWidth:640, width:'100%', height:this.state.iFrameHeight, overflow:'auto'}}>
{this.renderHTMLFrame()}
</div>
);
}
Run Code Online (Sandbox Code Playgroud)