滚动React JS

时间:2016-09-26 07:08:25

标签: javascript reactjs

有没有办法在React JS 没有 JQuery的点击事件中滚动创建动画?

我有导航,我想根据点击的用户转到页面部分。我想用一些动画来做。

问题是导航和部分不在同一个文件中。

我在主要组件中有这样的东西:

render() {
    return (
        <div>
            <Header currentLanguage={this.props.language.labels}/>
            {React.cloneElement(this.props.children, {
                currentLanguage: this.props.language.labels
            })}
            <Footer currentLanguage={this.props.language.labels}/>
        </div>
    );
}

在这个道具中,孩子是我想要导航的部分。他们是:

return (
            <div className="homeMain">
                <section ref="home" className="marginOnXs">
                    <MainSlider />
                </section>
                <section id="performance" ref="performance">
                    <Performance currentLanguage={languageHome}/>
                </section>
                <section id="benefits" ref="benefits">
                    <Benefits currentLanguage={languageHome} />
                </section>
                 <section ref="contact" id="contact">
                        <ContactForm currentLanguage={languageHome}/>
                 </section>
            </div>
        );

内部标题我有这样的事情:

<ul className="noPadding">
     <li> <Link to="" onClick={this.handleScroll.bind(this, "home")}>Home </Link></li>
     <li> <Link to="" onClick={this.handleScroll.bind(this, "contact")}>Contact?</Link></li>
 </ul>

在句柄滚动中我尝试了类似的东西:

handleScroll(id, event){
    event.preventDefault();
    let test = ReactDom.findDOMNode(this.refs[id]);
    let scrollTop = event.target.body.scrollTop,
    itemTranslate = Math.min(0, scrollTop/3 - 60);
    window.scrollTo(0, itemTranslate);
}

但它不起作用。 this.refs[id]返回undefined。如果单击标题中的联系人链接,则函数中的参数ID为contact,但找不到this.refs["contact"],因为它位于其他组件中。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用props传递函数,并将其用作兄弟组件之间的通信方式。

您可以在根组件中使用“this.props.handleScroll”并传递id以滚动到子项。像,

    <ul className="noPadding">
      <li> <Link to="" onClick={this.props.handleScroll.bind(this, "home")}>Home </Link></li>
      <li> <Link to="" onClick={this.props.handleScroll.bind(this, "contact")}>Contact?</Link></li>
    </ul>

   <-- In your root component --->
    <Header currentLanguage={this.props.language.labels} handleScroll={::this.handleScroll}} />

    handleScroll(id, event) {
      this.setState({
        scrollToId: id
      })
    }

    render() {
        return (
            <div>
                <Header currentLanguage={this.props.language.labels}/>
                {React.cloneElement(this.props.children, {
                    currentLanguage: this.props.language.labels,
                    scrollToId: this.state && this.state.scrollToId
                })}
                <Footer currentLanguage={this.props.language.labels}/>
            </div>
        );
    }

    <-- In your child component -->

    componentDidUpdate() {
      const idToScroll = this.props.scrollToId;
      // you can access this.refs[idToScroll] here and get the 
      // scrollheight and scroll to the position of the element
    }

    return (
          <div className="homeMain">
              <section ref="home" className="marginOnXs">
                  <MainSlider />
              </section>
              <section id="performance" ref="performance">
                  <Performance currentLanguage={languageHome}/>
              </section>
              <section id="benefits" ref="benefits">
                  <Benefits currentLanguage={languageHome} />
              </section>
               <section ref="contact" id="contact">
                      <ContactForm currentLanguage={languageHome}/>
               </section>
          </div>
    );

我认为你应该通过这个来更好地了解组件之间的沟通,React.js - Communicating between sibling componentsReact.js - Communicating between sibling components的答案