Vue method scroll div to top

后端 未结 5 1007
离开以前
离开以前 2021-01-18 06:20

I am learning vue. I have the following method where I add a chat message to a div with id=\"toolbar-chat\". This div allows scrolling on y axis an

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-18 06:57

    If you want to scroll with animated way;

    • Add scrollTo with an object parameter
    • Check your ref element ( in my case $refs[refName][0] )
    • If you need to use scroll one more place, add into global scope. (Vue.prototype.$toScroll) then use ( $toScroll() )

    Here is the basic usage.

    
    
    
    ...

    This is the method, you can use everywhere.

    toScroll(refName,position){
       this.messages.unshift(message);
       this.$nextTick(() => {
           this.$refs[refName][0].scrollTo({ top: position, behavior: 'smooth' });
       });
    }
    

提交回复
热议问题