1、功能代码
const scrollIntoView = (className = "is-error", block = "center") => {
Promise.resolve().then(() => {
const target = document.getElementsByClassName(className);
if (target.length) {
target[0].scrollIntoView({
behavior: "smooth", // 平滑过渡 值有auto、instant,smooth,缓动动画(当前是慢速的)
block: block, // 上边框与视窗顶部平齐。默认值 值有start,center,end,nearest,当前显示在视图区域中间
});
}
});
};
2、示例
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert("submit!");
} else {
scrollIntoView();
return false;
}
});
},
},