【问题标题】:scroll function triggering before reaching end point?在到达终点之前触发滚动功能?
【发布时间】:2018-06-07 18:29:35
【问题描述】:
import React, { Component } from 'react';
import './App.css';

import Home from './Components/Home/Home';
import Explainers from './Components/Explainers/Explainers';
import VideoCreation from './Components/VideoCreation/VideoCreation';
import Video from './Components/Video/Video';
import Footer from './Components/Footer/Footer';


class App extends Component {
  constructor(props){
    super(props);
    this.state = {isExplainers:false, isVideoExp:false }
    this.handleScroll = this.handleScroll.bind(this);
  }
   componentDidMount() {
    window.addEventListener('scroll', this.handleScroll);
  }
   componentWillUnMount() {
    window.removeEventListener('scroll', this.handleScroll);
  }

  handleScroll=()=>{
    console.log("scroll handle")
    this.setState({isExplainers:true});
    window.addEventListener('scroll', this.videoScroll);
  }
  videoScroll = () =>{
    console.log("scroll of Video");
    this.setState({isVideoExp:true});
    window.addEventListener('scroll', this.ourVideoScroll);
  }
  ourVideoScroll=()=>{
    console.log("our Video Scroll");
  }
  render() {
    const explainersClass = this.state.isExplainers ? "explainerAfter" : "explainer";
     const creationClass = this.state.isVideoExp ? "videoCreationAfter" : "videoCreation";
     const ourVideoClass = this.state.isExplainers ? "videoCreationAfter" : "videoCreation";
    return (

      <div className="App">
      <Home  onScroll = {this.handleScroll}/>
      <div className={explainersClass} onScroll={this.videoScroll}><Explainers /></div>
      <div className={creationClass} onScroll={this.ourVideoScroll}><VideoCreation /></div>
      <div className={ ourVideoClass } > <Video /></div>
      <Footer /> 
      </div>
    );
  }
}

export default App;

在这我有三个 onScroll 功能,我需要一个接一个工作的功能,一旦它到达组件的末尾就应该更新所有我的代码中的任何错误都会立即更新?或使用其他框架或其他任何其他形式或方法来做到这一点?

【问题讨论】:

    标签: javascript reactjs onscroll onscrolllistener


    【解决方案1】:

    你不需要为每个函数添加滚动事件,你可以从前一个函数中调用它。此外,由于 setState 是异步的,您可以从 setState 回调中调用这些函数,该回调在 setState 完成后执行

    class App extends Component {
      constructor(props){
        super(props);
        this.state = {isExplainers:false, isVideoExp:false }
        this.handleScroll = this.handleScroll.bind(this);
      }
       componentDidMount() {
        window.addEventListener('scroll', this.handleScroll);
      }
       componentWillUnMount() {
        window.removeEventListener('scroll', this.handleScroll);
      }
    
      handleScroll=(e)=>{
        console.log("scroll handle");
        const explainer = React.findDOMNode(this.explainer);
        const home = React.findDOMNode(this.home);
        if(home.scrollTop === explainer.offsetTop) { 
           this.setState({ isExplainers : true });
        }
      }
      videoScroll = () => {
        const explainer = React.findDOMNode(this.explainer);
        const video = React.findDOMNode(this.video);
        if(explainer.scrollTop === video.offsetTop) { 
           this.setState({ isVideoExp : true });
        }
      }
      ourVideoScroll=()=>{
        console.log("our Video Scroll");
        const ourVideo = React.findDOMNode(this.ourVideo);
        const video = React.findDOMNode(this.video);
        if(video.scrollTop === ourVideo.offsetTop) { 
           // take action here
        }
      }
      render() {
        const explainersClass = this.state.isExplainers ? "explainerAfter" : "explainer";
         const creationClass = this.state.isVideoExp ? "videoCreationAfter" : "videoCreation";
         const ourVideoClass = this.state.isExplainers ? "videoCreationAfter" : "videoCreation";
        return (
    
          <div className="App">
          <Home  ref={ref => this.home = ref} onScroll = {this.handleScroll}/>
          <div className={explainersClass} ref={ref => this.explainer = ref} onScroll={this.videoScroll}><Explainers /></div>
          <div className={creationClass} ref={ref => this.video = ref} onScroll={this.ourVideoScroll}><VideoCreation /></div>
          <div className={ ourVideoClass } ref={ref => this.ourVideo = ref}> <Video /></div>
          <Footer /> 
          </div>
        );
      }
    }
    
    export default App;
    

    【讨论】:

    • 让我试试这个
    • 它在到达组件末尾甚至开始之前立即执行
    • 预期的行为是什么,还有为什么你在所有 3 个 div 上都有 onScoll,如果它是一个柯里化功能,你最好只拥有一次,如果你希望它在滚动时执行到达组件端,可能需要对比scrollPosition和组件偏移+高度
    • 我正在寻找的功能是,如果滚动一旦到达解释器类,那么它应该更新我在函数中编写的下一个状态,但是如果我将一个函数放在另一个函数中,这一切都会触发跨度>
    • 现在只有第一个函数被其他两个不调用
    猜你喜欢
    • 1970-01-01
    • 2023-01-27
    • 2021-01-09
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多