js原生执行页面自动滚动方法

519 阅读1分钟

记录一下页面自动滚动方法

首先实现自动滚动方法

  // 滚动代码
  const scrollToTop = () =>{
    var hScrollTop = document.getElementById('app').scrollTop;
    var hScrollHeight = document.getElementById('app').scrollHeight;
    var height = 1
    console.log(hScrollTop+" "+hScrollHeight+" "+height);
    if((height+hScrollTop)>=hScrollHeight){//滚动条已经到了容器底部
      document.getElementById('app').scrollTop=0;
    }else{
      var h = hScrollTop + height;
      document.getElementById('app').scrollTop=h;
      window.requestAnimationFrame(scrollToTop)
    }
  }

之后调节每次滚动高度

 var height = 1

最后在执行方法

    scrollToTop()

这样就页面自动滚动了 -----记录一下