滚动轴滚动方向判断

开始时的判断代码,通过在滚动函数当中再监听一个滚动函数,然后判断前后的差值来判断是向上滚动还是向下滚动

changeIsDownStatus = () => {
    const listView = this.listView;
    const beforeScrollTop = listView.scrollTop;
    this.listView.addEventListener('scroll', () => {
      const afterScrollTop = listView.scrollTop;
      this.isDown = afterScrollTop > beforeScrollTop;
    });
  };

优化后的方法,通过当前的值减去缓存的值,然后再把缓存的值更新为当前的这种方式来判断滚动的方向,这种方式的性能比上面那种更好,同时代码更加的简洁

changeIsDownStatus = () => {
    const listView = this.listView;
    this.isDown = listView.scrollTop > this.beforeScrollTop;
    this.beforeScrollTop = listView.scrollTop;
    return this.isDown;
  };
原文地址:https://www.cnblogs.com/kugeliu/p/9205299.html