How to achieve automatic focus and jump in vue?

as shown in the figure, v-for generates input, in Vue. When the add button is clicked, the cursor automatically focuses on the latest input, window and automatically moves to the appropriate location!

Feb.27,2021

get the last dom element and call the focus function


vue.js 's official Chinese tutorial [Custom instructions] (the https://cn.vuejs.org/v2/guide/custom-directive.html) section happens to be very similar to your situation, which, of course, is essentially a direct call to focus to get focus. As for the scrolling window, use window.scrollTo to make sure that (. Scroll -) scroll

specific scroll where to go, the calculation idea is to obtain the following two key values:

  1. screen size;
  2. the location of the element you want to display

if you just want to make sure the vertical position is right, just get the vertical value. The second one seems to be difficult to obtain. I checked it for you and read it for yourself.

  1. https://www.cnblogs.com/wujin.
  2. https://www.cnblogs.com/wujin.

but it just occurred to me that you can add a name to the element you want to move to the center and then window.location.href + ='- sharpname' something like this. Key words: anchor point. If the scrolling position is still inappropriate-- it usually goes up-- you can get the current window scrolling height at the same time, and use window.scrollTo to scroll down a little bit (for example, scroll the height of half the screen).

Menu