示例:vxe-table,vue3 框架 ,滚动到今天日期为例
import {ref, watchEffect} from 'vue'
//等页面渲染完之后在执行计算逻辑
const loading =ref('no')
// 初始化滚动位置
watchEffect(() => {
// 计算横向滚动条滚动位置
if (loading .value == 'yes') {
setTimeout(() => {
// 获取当前列(可以在数据里面做好你要滚动到那一列标识)
const current = dayjs(new Date()).format('YYYY-MM-DD')
//获取今天之前的所有日期元素的宽度
const currentArr = gridOptions.columns.filter((item) => item.field < current)
//currentArr.length 需要滚动列的条数,100:一列的宽度
document.querySelector('.vxe-table--body-wrapper')?.scrollTo(currentArr.length * 100, 0)
//纵向滚条条原理一样
//scrollTo(x,y)x:横向滚动条的位置,y:纵向滚动条的位置
}, 50)
}
})