admin管理员组文章数量:1023867
I made a modal windows using react-bootstrap-sweetalert
lib.
It contains long list of contents, so I allowed overflow:scroll
.
What the problem is, when modal open, it doesn't scroll to top.
And scroll to unknown position, so I need to scroll to top manually.
This is simple code
basicAlert = () => {
this.setState({
alert: (
<div>
// long list table
</div>)
});
}
hideAlert = () => {
this.setState({
alert: null
});
}
render() {
return (
{this.state.alert}
// rest contents ...
)
}
Any advice will be big help for me. Thanks
I made a modal windows using react-bootstrap-sweetalert
lib.
It contains long list of contents, so I allowed overflow:scroll
.
What the problem is, when modal open, it doesn't scroll to top.
And scroll to unknown position, so I need to scroll to top manually.
This is simple code
basicAlert = () => {
this.setState({
alert: (
<div>
// long list table
</div>)
});
}
hideAlert = () => {
this.setState({
alert: null
});
}
render() {
return (
{this.state.alert}
// rest contents ...
)
}
Any advice will be big help for me. Thanks
Share Improve this question asked Sep 7, 2018 at 4:33 Nomura NoriNomura Nori 5,1979 gold badges56 silver badges91 bronze badges1 Answer
Reset to default 2You could create a ref
to an element in your ponent that wraps the scrollable content, and then use this ref to set scrollTop
to 0
of the corresponding DOM element, when content is displayed in your modal.
So for instance, the following additions/adjustments to your ponent should achieve what you need:
// Add a constructor to create the ref
constructor(props) {
super(props)
// Add a ponent ref to your ponent. You'll use this to set scroll
// position of div wrapping content
this.ponentRef = React.createRef();
}
basicAlert = () => {
this.setState({
alert: (
<div>
// long list table
</div>)
}, () => {
// After state has been updated, set scroll position of wrapped div
// to scroll to top
this.ponentRef.current.scrollTop = 0;
});
}
render() {
// Register your ref with wrapper div
return (<div ref={ this.ponentRef }>
{ this.state.alert }
// rest contents ...
</div>)
}
I made a modal windows using react-bootstrap-sweetalert
lib.
It contains long list of contents, so I allowed overflow:scroll
.
What the problem is, when modal open, it doesn't scroll to top.
And scroll to unknown position, so I need to scroll to top manually.
This is simple code
basicAlert = () => {
this.setState({
alert: (
<div>
// long list table
</div>)
});
}
hideAlert = () => {
this.setState({
alert: null
});
}
render() {
return (
{this.state.alert}
// rest contents ...
)
}
Any advice will be big help for me. Thanks
I made a modal windows using react-bootstrap-sweetalert
lib.
It contains long list of contents, so I allowed overflow:scroll
.
What the problem is, when modal open, it doesn't scroll to top.
And scroll to unknown position, so I need to scroll to top manually.
This is simple code
basicAlert = () => {
this.setState({
alert: (
<div>
// long list table
</div>)
});
}
hideAlert = () => {
this.setState({
alert: null
});
}
render() {
return (
{this.state.alert}
// rest contents ...
)
}
Any advice will be big help for me. Thanks
Share Improve this question asked Sep 7, 2018 at 4:33 Nomura NoriNomura Nori 5,1979 gold badges56 silver badges91 bronze badges1 Answer
Reset to default 2You could create a ref
to an element in your ponent that wraps the scrollable content, and then use this ref to set scrollTop
to 0
of the corresponding DOM element, when content is displayed in your modal.
So for instance, the following additions/adjustments to your ponent should achieve what you need:
// Add a constructor to create the ref
constructor(props) {
super(props)
// Add a ponent ref to your ponent. You'll use this to set scroll
// position of div wrapping content
this.ponentRef = React.createRef();
}
basicAlert = () => {
this.setState({
alert: (
<div>
// long list table
</div>)
}, () => {
// After state has been updated, set scroll position of wrapped div
// to scroll to top
this.ponentRef.current.scrollTop = 0;
});
}
render() {
// Register your ref with wrapper div
return (<div ref={ this.ponentRef }>
{ this.state.alert }
// rest contents ...
</div>)
}
本文标签: javascriptHow to set modal scroll to top when it appears in ReactjsStack Overflow
版权声明:本文标题:javascript - How to set modal scroll to top when it appears in React.js - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745518897a2154224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论