用 Vue.js 滚动到 div 的底部

我有一个 Vue.js 组件,其中包含几个元素。当组件中的方法被调用时,我想自动滚动到该元素的底部。

基本上,做与 这个相同的事情。但是,我还没有找到一种方法来获取组件中的元素并修改 scrollTop

我目前正在使用 Vue.js 2.0.8。

220648 次浏览

In the related question you posted, we already have a way to achieve that in plain javascript, so we only need to get the js reference to the dom node we want to scroll.

The ref attribute can be used to declare reference to html elements to make them available in vue's component methods.

Or, if the method in the component is a handler for some UI event, and the target is related to the div you want to scroll in space, you can simply pass in the event object along with your wanted arguments, and do the scroll like scroll(event.target.nextSibling).

As I understand, the desired effect you want is to scroll to the end of a list (or scrollable div) when something happens (e.g.: an item is added to the list). If so, you can scroll to the end of a container element (or even the page it self) using only pure JavaScript and the VueJS selectors.

var container = this.$el.querySelector("#container");
container.scrollTop = container.scrollHeight;

I've provided a working example in this fiddle.

Every time a item is added to the list, the list is scrolled to the end to show the new item.

Hope this helps you.

I tried the accepted solution and it didn't work for me. I use the browser debugger and found out the actual height that should be used is the clientHeight BUT you have to put this into the updated() hook for the whole solution to work.

data(){
return {
conversation: [
{
}
]
},
mounted(){
EventBus.$on('msg-ctr--push-msg-in-conversation', textMsg => {
this.conversation.push(textMsg)
// Didn't work doing scroll here
})
},
updated(){              <=== PUT IT HERE !!
var elem = this.$el
elem.scrollTop = elem.clientHeight;
},

Here is a simple example using ref to scroll to the bottom of a div.

/*
Defined somewhere:
var vueContent = new Vue({
el: '#vue-content',
...
*/


var messageDisplay = vueContent.$refs.messageDisplay;
messageDisplay.scrollTop = messageDisplay.scrollHeight;
<div id='vue-content'>
<div ref='messageDisplay' id='messages'>
<div v-for="message in messages">
\{\{ message }}
</div>
</div>
</div>

Notice that by putting ref='messageDisplay' in the HTML, you have access to the element through vueContent.$refs.messageDisplay

I had the same need in my app (with complex nested components structure) and I unfortunately did not succeed to make it work.

Finally I used vue-scrollto that works fine !

2022 easy, readable, smooth scrolling ability, & won't hurt your brain... use el.scrollIntoView()

scrollIntoView() has options you can pass it like scrollIntoView({behavior: 'smooth'}) to get smooth scrolling out of the box and does not require any external libraries.

Here is a fiddle.

methods: {
scrollToElement() {
const el = this.$refs.scrollToMe;


if (el) {
// Use el.scrollIntoView() to instantly scroll to the element
el.scrollIntoView({behavior: 'smooth'});
}
}
}

Then if you wanted to scroll to this element on page load you could call this method like this:

mounted() {
this.scrollToElement();
}

Else if you wanted to scroll to it on a button click or some other action you could call it the same way:

<button @click="scrollToElement">scroll to me</button>

The scroll works all the way down to IE 8. The smooth scroll effect does not work out of the box in IE or Safari. If needed there is a polyfill available for this here as @mostafaznv mentioned in the comments.

  1. Use the ref attribute on the DOM element for reference
<div class="content scrollable" ref="msgContainer">
<!-- content -->
</div>
  1. You need to setup a WATCH
  data() {
return {
count: 5
};
},
watch: {
count: function() {
this.$nextTick(function() {
var container = this.$refs.msgContainer;
container.scrollTop = container.scrollHeight + 120;
});
}
}
  1. Ensure you're using proper CSS
.scrollable {
overflow: hidden;
overflow-y: scroll;
height: calc(100vh - 20px);
}

If you need to support IE11 and (old) Edge, you can use:

scrollToBottom() {
let element = document.getElementById("yourID");
element.scrollIntoView(false);
}

If you don't need to support IE11, the following will work (clearer code):

scrollToBottom() {
let element = document.getElementById("yourID");
element.scrollIntoView({behavior: "smooth", block: "end"});
}

The solution did not work for me but the following code works for me. I am working on dynamic items with class of message-box.

scrollToEnd() {
setTimeout(() => {
this.$el
.getElementsByClassName("message-box")
[
this.$el.getElementsByClassName("message-box").length -
1
].scrollIntoView();
}, 50);
}

Remember to put the method in mounted() not created() and add class message-box to the dynamic item.

setTimeout() is essential for this to work. You can refer to https://forum.vuejs.org/t/getelementsbyclassname-and-htmlcollection-within-a-watcher/26478 for more information about this.

For those that haven't found a working solution above, I believe I have a working one. My specific use case was that I wanted to scroll to the bottom of a specific div - in my case a chatbox - whenever a new message was added to the array.

const container = this.$el.querySelector('#messagesCardContent');
this.$nextTick(() => {
// DOM updated
container.scrollTop = container.scrollHeight;
});

I have to use nextTick as we need to wait for the dom to update from the data change before doing the scroll!

I just put the above code in a watcher for the messages array, like so:

messages: {
handler() {
// this scrolls the messages to the bottom on loading data
const container = this.$el.querySelector('#messagesCard');
this.$nextTick(() => {
// DOM updated
container.scrollTop = container.scrollHeight;
});
},
deep: true,
},

This is what worked for me

this.$nextTick(() => {
let scrollHeight = this.$refs.messages.scrollHeight
window.scrollTo(0, scrollHeight)
})

Try vue-chat-scroll:

Install via npm: npm install --save vue-chat-scroll

Import:

import Vue from 'vue'
import VueChatScroll from 'vue-chat-scroll'
Vue.use(VueChatScroll)

in app.js after window.Vue = require('vue').default;

then use it with :

<ul class="messages" v-chat-scroll>
// your message/chat code...
</ul>

My solutions without modules:

Template

<div class="scrollable-content" ref="conversations" />

Script

scrollToBottom() {
const container = this.$refs.conversations;


container.scrollTop = container.scrollHeight;
},
scrollToBottom() {
this.$nextTick(function () {
let BoxEl = document.querySelector('#Box');
if(BoxEl)
BoxEl.scrollTop = BoxEl.scrollHeight;
});
}

Agree with Lurein Perera

Just want to add extra info

watch: {
arrayName: {
handler() {
const container = this.$el.querySelector("#idName");
this.$nextTick(() => {
container.scrollTop = container.scrollHeight;
});
},
deep: true,
},
},

Where as:

arrayName = Name of array

idName = The id attribute has to be added to the div where you want the scrollbar to auto-scroll down when arrayName length increases.

  scrollToElement() {
const element = this.$refs.abc; // here abc is the ref of the element


if (element) {
el.scrollIntoView({behavior: 'smooth'});
}
}
}

here you need to use ref for the particular div or element which you want make visible on scroll.

if you have a table and you want to locate the last row of the table then you have to use -

element.lastElementChild.scrollIntoView({behaviour:'smooth'})

Here not that if you ware asynchronously adding the element to the table then you have to take care of it. you can test it using setTimeout, if that is making any difference.

e.g.

    const element = this.$refs.abc;
if (element) {
setTimeout(() => {
element.lastElementChild.scrollIntoView({behaviour:'smooth'})
}, 1000);


}
}

replace set timeout with your own async logic.

Using Composition API and TypeScript


I set the parameter scrollTop equal to scrollHeightfrom the HTMLDivElment API.

    <template>
<div id="container" ref="comments">
Content ...
</div>
</template>
<script lang="ts">
import { defineComponent, ref, Ref, watchEffect } from 'vue'


export default defineComponent({
setup() {
const comments: Ref<null | HTMLDivElement> = ref(null)


watchEffect(() => {
if(comments.value) {
comments.value.scrollTop = comments.value.scrollHeight
}
})


return {
comments
}
}
})
</script>