Reputation: 760
Is there any way that I can scrollTo first input which have invalid class using VueJs. Right now I am using window.scrollTo(500, 0);
which is obviously not a proper solution.
Upvotes: 0
Views: 2962
Reputation: 4839
You'll probably need to use Vue.nextTick to wait for Vue to apply your invalid class everywhere it needs to be applied.
this.$nextTick(() => {
let domRect = document.querySelector('.error').getBoundingClientRect();
window.scrollTo(
domRect.left + document.documentElement.scrollLeft,
domRect.top + document.documentElement.scrollTop
);
});
Upvotes: 5
Reputation: 1
One more way :)
this.$nextTick(() => {
document.querySelector('.error').scrollIntoView({block: "center", behavior: "smooth"})
});
Upvotes: 0
Reputation: 5577
Just get first elements with error position:
window.scrollTo(document.querySelector('.error').offsetTop, 0);
Upvotes: 1