gailiuzi

项目问题

在做监听浏览器屏幕变化时,尤其是屏幕上下滑动时,要方便用户,添加一个返回顶部的功能,pc端还好,但是在移动端会根据不用的手机会有兼容性的问题,所以就要进行一个判断来进行这样一个功能

首先需要一个计时器

this.timer = setInterval(this.play,1);

然后声明计时器里定义的方法函数,在方法里对屏幕动态进行监听,因为兼容性的问题还要进行判断

play(){
    this.winScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    console.log(this.winScrollTop)
    console.log( document.body.scrollTop)
    // this.winScrollTop -= 5
    if(document.documentElement.scrollTop == 0) {
      document.body.scrollTop=0
      if ( document.body.scrollTop <= 0) {
        clearInterval(this.timer)
      }
    }else {
      document.documentElement.scrollTop =0
      if (document.documentElement.scrollTop <= 0) {
        clearInterval(this.timer)
      }
    }
  },

 

posted on 2022-04-07 21:00  代码界街溜子  阅读(17)  评论(0编辑  收藏  举报