加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React-listen滚动向上/向下?

发布时间:2020-12-15 20:41:31 所属栏目:百科 来源:网络整理
导读:我正在构建一个应用程序(ES6),我很好奇什么是正确的方法来捕捉向上/向下滚动事件? 我试过(npm)安装react-scroll-listener但我无法使用我的ES6类. 我基本上想要:如果向上滚动,请执行此操作;如果向下滚动,请执行其他操作. import React from 'react';import
我正在构建一个应用程序(ES6),我很好奇什么是正确的方法来捕捉向上/向下滚动事件?

我试过(npm)安装react-scroll-listener但我无法使用我的ES6类.

我基本上想要:如果向上滚动,请执行此操作;如果向下滚动,请执行其他操作.

import React from 'react';
import config from '../config';
import StaticImageList from '../Common/StaticImageList';
import ScrollListener from 'react-scroll-listener';

class Album extends React.Component {
    constructor(props){
      super(props);

    }
    myScrollStartHandler(){
        console.log('Scroll start');

    }
    myScrollEndHandler(){
        console.log('Scroll end 300ms(default)');
    }
    componentDidMount(){
      scrollListener.addScrollHandler('body',myScrollStartHandler,myScrollEndHandler );
    }
    render(){
      return <StaticImageList />;
    }
};

export default Album;
这是挂钩任何听众的一般建议:

在componentDillMount中附加内容,在componentWillUnmount中取消附加

class Whatever extends Component {
  componentDidMount() {
    window.addEventListener('scroll',this.handleScroll,{ passive: true })
  }

  componentWillUnmount() {
    window.removeEventListener('scroll',this.handleScroll)
  }

  handleScroll(event) {
    // do something like call `this.setState`
    // access window.scrollY etc
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读