Vue sets the entire page back to the top and the partial div back to the top

Method 1: You can set it like this when the entire page scrolls

​

mounted(){
  window.addEventListener('scroll', this.handleScroll)
},
methods :(
 handleScroll() {
   this .scrolltop = window.pageyoffset||document.documentElement.scrollTop||document 
   .body.scrollTop
 },
 gotop(){
  let timer = setInterval(() => {
    let ostop = document.documentElement.scrollTop || document.body.scrollTop
    let ispeed = Math.floor(-ostop/5)
    document.documentElement.scrollTop = document.body.scrollTop = ostop+ispeed
    if(ostop ==-0){
      clearInterval(timer)
    }
  },30)
 }
}

​

 

 Method 2: A certain box in a page partially scrolls back to the top

Find the parent box that needs to scroll the content box

let p = document.querySelector('.van-tabs_content')
this.$nextTick(() => {
  b.scrollTop = 0
})
// 一定要加nextTick 

Guess you like

Origin blog.csdn.net/weixin_54368936/article/details/130267982