小程序:scroll-view設(shè)定滾動條到指定位置

mpvue開發(fā)小程序
scroll-into-view 是 scroll-view 的一個(gè)屬性 , 其綁定的是一個(gè)變量 , 即要定位的子節(jié)點(diǎn)的id ( id不能以數(shù)字開頭 )

  • scroll-view 必須有固定的高度
  • scroll-into-view 必須是變量是變量是變量
 <scroll-view class="chat-list" :scroll-y="true" :scroll-into-view="toView">
   <view v-for="(item,index) in messageList" :key="index" :id="'msg-'+index">
      <div v-if="item.username == item.receiveUser">Demo1</div>
      <div v-else>Demo2</div>
   </view>
</scroll-view>

<script>
   export default{
      data(){
        // 消息列表
        messageList:[],
        // 滾動條定位位置
        toView: ''
      },
      methods: {
        setScrollPoint(){ //在合適的時(shí)機(jī)調(diào)用即可
          //以下設(shè)法是給變量賦值最后一個(gè)節(jié)點(diǎn)的id(將滾動條設(shè)到最后一條消息位置)
          this.toView = 'msg-' + (this.messageList.length - 1);
        }
      }
   }
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容