vue进入页面时滚动条始终在底部

mounted () {
   this.scrollToBottom();
},
//每次页面渲染完之后滚动条在最底部
updated:function(){
   this.scrollToBottom();
},
methods:{
   scrollToBottom: function () {
     this.$nextTick(() => {
	    var container = this.$el.querySelector(".chatBox-content-demo");
        container.scrollTop = container.scrollHeight;
     })
}
}

滚动条样式:

::-webkit-scrollbar {
    width: 5px;
    height: 5px;
}

::-webkit-scrollbar-track-piece {
    background-color: rgba(0, 0, 0, 0.2);
    -webkit-border-radius: 6px;
}

::-webkit-scrollbar-thumb:vertical {
    height: 5px;
    background-color: rgba(125, 125, 125, 0.7);
    -webkit-border-radius: 6px;
}

::-webkit-scrollbar-thumb:horizontal {
    width: 5px;
    background-color: rgba(125, 125, 125, 0.7);
    -webkit-border-radius: 6px;
}

如果使用了better-scroll

在created()或者mounted () 里都可以

mounted () {
  this.$nextTick(() => {
  this._initScroll();
  let allLi=this.$refs.chatcontheight.querySelectorAll("li");
  this.chartScroll.scrollToElement(allLi[allLi.length-1],0);
  });
},
//每次页面渲染完之后滚动条在最底部
updated:function(){
   this.$nextTick(() => {
this._initScroll();
let allLi=this.$refs.chatcontheight.querySelectorAll("li");
this.chartScroll.scrollToElement(allLi[allLi.length-1],0);
   });
},
methods:{
  _initScroll(){
this.chartScroll=new BScroll(this.$refs.chatcontheight,{
     click:true
   })
}}

结构

你可能感兴趣的:(vue)