Javascript 如何偏移scrollIntoView()
在一个React应用程序上工作,有这个功能可以滚动到一个div,但它只是稍微关闭,所以当它转到正确的div时,我需要让它向上滚动20px左右Javascript 如何偏移scrollIntoView(),javascript,reactjs,Javascript,Reactjs,在一个React应用程序上工作,有这个功能可以滚动到一个div,但它只是稍微关闭,所以当它转到正确的div时,我需要让它向上滚动20px左右 class CountryDropdown extends Component{ handleScroll = (country) => { document.getElementById(country).scrollIntoView({ block: "start",
class CountryDropdown extends Component{
handleScroll = (country) => {
document.getElementById(country).scrollIntoView({
block: "start",
behavior: "smooth"
})
}
render () {
return (
<Dropdown text='Jump to Country'>
<Dropdown.Menu>
<Dropdown.Item flag='fr' text='France' onClick={() => this.handleScroll("france")}/>
<Dropdown.Item flag='es' text='Spain' onClick={() => this.handleScroll("spain")}/>
<Dropdown.Item flag='pt' text='Portugal' onClick={() => this.handleScroll("portugal")}/>
<Dropdown.Item flag='gr' text='Greece' onClick={() => this.handleScroll("greece")}/>
<Dropdown.Item flag='de' text='Germany' onClick={() => this.handleScroll("germany")}/>
<Dropdown.Item flag='at' text='Austria' onClick={() => this.handleScroll("austria")}/>
</Dropdown.Menu>
</Dropdown>
);
}
}
export default CountryDropdown;
class CountryDropdown扩展组件{
handleScroll=(国家)=>{
document.getElementById(国家)。scrollIntoView({
块:“开始”,
行为:“平稳”
})
}
渲染(){
返回(
这个.handleScroll(“法国”)}/>
this.handleScroll(“西班牙”)}/>
this.handleScroll(“葡萄牙”)}/>
this.handleScroll(“希腊”)}/>
this.handleScroll(“德国”)}/>
这个.handleScroll(“奥地利”)}/>
);
}
}
导出默认国家下拉列表;
你好,伊莱恩!刚刚给你写了一个答案,如果你有任何问题,请告诉我。嗨@ChristophenGo,我以前试过,但似乎不起作用。我想知道是不是因为React是异步的?我试着用一个.then语句来包装它,但没能让它起作用。。。