码迷,mamicode.com
首页 > 其他好文 > 详细

滚动轴滚动方向判断

时间:2018-06-20 21:34:00      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:after   status   res   for   this   list   const   style   lis   

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

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;
  };

 

滚动轴滚动方向判断

标签:after   status   res   for   this   list   const   style   lis   

原文地址:https://www.cnblogs.com/kugeliu/p/9205299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!