场景: 选择时页面滚动到对应到div。
scrollToAnchor = anchorName => {
if (anchorName) {
const anchorElement = document.getElementById(anchorName);
if (anchorElement) {
try {
// chrome 61开始才兼容scrollIntoView(scrollIntoViewOption:object)的写法
anchorElement.scrollIntoView({ behavior: "smooth", block: "start" });
} catch (e) {
// 低版本chrome(<61)
anchorElement.scrollIntoView();
}
}
}
};
handleChange = value => {
this.scrollToAnchor(value);
};
<Select defaultValue="111" style={{ width: 120 }} onChange={this.handleChange}>
<Option value="111">111</Option>
<Option value="222">222</Option>
</Select>
<div style={{ height: 500 }} id="111">1111的内容</div>
<div style={{ height: 500 }} id="222">222的内容 </div>