简体   繁体   English

React Bootstrap Modal滚动到顶部

[英]React Bootstrap Modal Scroll to Top

I have React Bootstrap modal that contains a form. 我有包含表单的React Bootstrap模式。 When the form validates, it displays the errors at the top of the form. 表单验证时,它会在表单顶部显示错误。 Because the form is large, these errors are not visible. 由于表单很大,因此这些错误不可见。

I need the modal to scroll back to the top. 我需要模态滚动回到顶部。

I have tried attaching a ref to the modal and using scrollTop but it doesn't seem to work - like this 我已经尝试将ref附加到模态并使用scrollTop但它似乎不起作用 - 像这样

<Modal ref={el => (this.modal = el)} ......... />

Then; 然后; this.modal.scrollTop = 0

You can use scrollTop only for the DOM element and not the React component class, To get the DOM element make use of ReactDOM.findDOMNode() like 你可以只使用scrollTop作为DOM元素,而不是React组件类。要获得DOM元素,请使用ReactDOM.findDOMNode()类的

<Modal ref={el => (this.modal = el)} ......... />

const modalDOM = ReactDOM.findDOMNode(this.modal);
modalDOM.scrollTop = 0

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

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