简体   繁体   中英

Scroll to bottom in Ionic Vue

I develop a chat app in Ionic Vue. To always see the latest messages, I have to scoll to the bottom automatically.

For web I use vue-chat-scroll, but this does not work with Ionic Vue.

I have tried a lot of different tactics but nothing works:

var objDiv = document.getElementById("your_div");
objDiv.scrollTop = objDiv.scrollHeight;

or

this.scrollIntoView(false);

or

var element = document.getElementById("scroll");
element.scrollIntoView();

or

vue-scroll-to with a hidden anchor at the end of the view

I have also tried different methods of listing the chat messages:

  • a combination of IonGrid with Rows
  • a normal ul / li list
  • just with divs

Take a look at "ion-content" component, https://ionicframework.com/docs/api/content

It has many methods and events for scrolling.

You only need to call the method .scrollToBottom() on the <ion-content> element. You can use the following approach, refering the ion-content with a ref and calling the method scrollToBottom bellow:

<template>
<ion-content ref="content">

</ion-content>
</template>

export default defineComponent({
  methods: {
    scrollToBottom(){
      this.$refs['content'].scrollToBottom()
    }
  },
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM