ScrollLeft无法与ScrollContainer配合使用

时间:2020-06-04 09:41:41

标签: javascript html reactjs dom

我使用了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>
 }
} 

0 个答案:

没有答案