我使用了react-indiana-drag-scroll库,并尝试将scrollLeft值传递给scrollContainer,以便ScrollContainer滚动到该值。根据console.log的输出,还传递了正确的值,但是滚动条的位置没有改变。
import React from 'react';
import ScrollContainer from 'react-indiana-drag-scroll';
class ProjectList extends React.Component{
constructor(props){
super(props);
this.scrollRef = React.createRef();
this.scrollTo = this.scrollTo.bind(this);
}
scrollTo(projectRef){
//Scroll horizontal bar to clicked Project
this.scrollRef.current.scrollLeft = projectRef.current.offsetLeft;
console.log(projectRef.current.offsetLeft);
console.log(this.scrollRef.current.scrollLeft);
//Outputs the same two value from projectRef.current.offsetLeft
}
render() {
<div>
<ScrollContainer className="scroll-container"
hideScrollbars={false}
horizontal={true}
ref = {this.scrollRef}>
{
this.props.projects.map(project => {
return <Project
key={project.name}
scrollTo={this.scrollTo}
/>
})
}
</ScrollContainer>
</div>
}
}