您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在ReactJS中将iframe的高度设置为scrollHeight

在ReactJS中将iframe的高度设置为scrollHeight

要做的是在您的componentDidMount中,运行脚本来设置高度。[如果要加载外部内容,则可能要在IFrame上添加事件侦听器,以等待加载外部内容

   componentDidMount() {
       const obj = ReactDOM.findDOMNode(this);
       obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
    }

还有另一种更“反应”的方式-将高度存储在状态中。

   componentDidMount() {
       const obj = ReactDOM.findDOMNode(this);
       this.setState({iFrameHeight:  obj.contentWindow.document.body.scrollHeight + 'px'});
    }

然后在您的渲染器中:

render() {
    return (
        <div style={{maxWidth:640, width:'100%', height:this.state.iFrameHeight, overflow:'auto'}}>
            {this.renderHTMLFrame()}
        </div>
    );
}
其他 2022/1/1 18:14:52 有1134人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶