vue3实现新增内容时,直接跳转到新增内容部分

点击按钮新增条件时,新增的内容会展示在最后,需要鼠标滑动才可以看到,所以想要实现点击新增条件时,页面直接跳转到新增内容。

主要代码如下:

<NButton type="primary" size="small" @click="addCondition">新增条件</NButton>
<NForm v-if="conditions.length > 0" ref="formRef2" :model="conditions" :rules="conditionsRules">
      <div v-for="(condition,index) in conditions" :key="index" :ref="(el) => setConditionRef(el, index)">
          <!-- 实际业务需求-->
      </div>
</NForm>
 const conditionRefs = ref([])
 // 收集动态生成的 div 引用
 const setConditionRef = (el, index) => {
   if (el) conditionRefs.value[index] = el
 }
const addCondition = async () => {
  const newCondition = {
	// 根据实际需求
  };

  conditions.value.push(newCondition);

  nextTick(() => {
    const lastIndex = conditions.value.length - 1
    conditionRefs.value[lastIndex]?.scrollIntoView({
      behavior: 'auto',
      block: 'center'
    })
  })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值