vue实现滚动回到顶部和input框自动获取焦点
<template>
<div class="preview" ref="scrollRef">
<input
type="text"
placeholder="请填写投放名称"
v-model="launchInfo.demandLaunchPlanName"
class="launch-name"
ref="input"
/>
</div>
<div @click="top">回到顶部并获取焦点</div>
</template>
methods: {
top(){
//获取需要回到顶部的节点的位置
let top = this.$refs.scrollRef.scrollTop
// 缓慢滚动回到顶部
const timeTop = setInterval(() => {
this.$refs.scrollRef.scrollTop = top -= 50
if (top <= 0) {
clearInterval(timeTop)
}
}, 30)
// input框获取焦点
this.$nextTick(() => {
this.$refs.input.focus();
})
}
}