element.scrollIntoView(
{
behavior: "auto", // smooth-平滑
block: "center", // 垂直方向的对齐方式 start center end nearest
inline: "start", // 控制水平方向的对齐方式 start center end nearest
}
)
<template>
<van-steps
v-model:active="activeStep"
ref="stepsRef"
@click-step="handleStepClick"
>
<van-step>步骤1</van-step>
<van-step>步骤2</van-step>
<van-step>步骤3</van-step>
</van-steps>
</template>
<script setup>
import { ref } from 'vue';
const activeStep = ref(0);
const stepsRef = ref(null);
const handleStepClick = (index) => {
activeStep.value = index;
scrollToActiveStep();
};
const scrollToActiveStep = () => {
nextTick(() => {
const activeStepNode = stepsRef.value?.$el.querySelector('.van-step--process');
if (activeStepNode) {
activeStepNode.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'nearest'
});
}
});
};
</script>