Can you post your entire component please ?
scrollIntoView on the search page
I have a simple problem, but I don’t know how to solve it.
My search page is done by axios, without realoading the page. When the user searches, the page must be scrolled down to the lowest id.
however, if a user searches and it returns nothing, the following query (which returns content) does not scroll down, as the gift has been dynamically updated. In this case, only the second click would take you down.
My code looks like this:
methods: {
buscar() {
axios.get('/api/buscarquery?lang='+document.documentElement.lang+'&chave='+this.keyword, {
})
.then(response => {
document.getElementById('buscou').scrollIntoView({ behavior: 'smooth' });
})
.catch( error => {
});
},
}
How do I make scrollIntoView work when the result is empty and, consequently, there is no scroll bar. Only after the data is inserted dynamically does the scroll bar appear, but my code doesn’t seem to recognize it at first, but on the second attempt.
can you help me ?
The problem is that this.maisVistos is being assigned an array of data fetched from the API, but the DOM doesn't actually refresh right away, so your scrollIntoView is actually executed before there is a scroll bar on the page. To solve that issue, you can wrap your scrollintoView logic in this.$nextTick's callback:
this.$nextTick(() => document.getElementById('buscou').scrollIntoView({ behavior: 'smooth' }));
this.$nextTick is only executed when the view has been re-rendered completely.
Side note: I would suggest using this.$refs.buscou instead of document.getElementById('buscou') to be in line with Vue standards. You will need to change id="buscou" to ref="buscou" in your HTML.
Please or to participate in this conversation.