Afa*_*han 4 javascript dom scroll reactjs react-component
在不使用任何其他库的情况下使用 id 在反应中滚动到特定的 div,我发现了一些他们使用滚动库的解决方案
这是我的WrappedQuestionFormFinal组件中的 div 代码
<div className="form-section">
<Row>
<Col xs={24} xl={4} className="form-section-cols">
<h3 id={id}>
{t('QUESTION')} {t(id + 1)}
</h3>
</Col>
</Row>
</div>
Run Code Online (Sandbox Code Playgroud)
它是通过这里组件调用的嵌套组件
<WrappedQuestionFormFinal
allQuestions={questions}
updateScreenTitle={this.props.updateScreenTitle}
handleDeleteQuestion={this.handleDeleteQuestion}
question={questions[q]}
dublicateQuestion={dubQuestion}
dependantQuestion={dependant}
id={i}
key={i}
tags={this.props.tags}
changeOrder={this.changeOrder}
changeScreenNo={this.changeScreenNo}
/>,
Run Code Online (Sandbox Code Playgroud)
我面临的问题是这个表单有大约 10 个问题,如果发生错误时提交,我必须滚动发生错误的页面。Id 被赋予 h1 标签,这是唯一的
Dre*_*ese 11
我使用 react refelement.scrollIntoView来实现这一点。
class App extends Component {
constructor(props) {
super(props);
this.scrollDiv = createRef();
}
render() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<button
onClick={() => {
this.scrollDiv.current.scrollIntoView({ behavior: 'smooth' });
}}
>
click me!
</button>
<h2>Start editing to see some magic happen!</h2>
<div ref={this.scrollDiv}>hi</div>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17310 次 |
| 最近记录: |