vue3滚动条重置

由于单页面应用的原理,使用vue搭建的项目在路由切换时并不会刷新整个页面。因此在有滚动条的页面会存在滚动条不能自动重置的问题。在最近的项目中就遇到了这种问题,因此记录一下。

1.切换路由:

<el-main>

<!--<router-view />-->

<router-view v-slot="{ Component }">

<transition name="fade-slide" mode="out-in" appear>

<component :is="Component" id="main" />

</transition>

</router-view>

</el-main>
onBeforeRouteUpdate((to, from) => {

// 由于路由设置了0.3s过渡效果,所以此处设置了0.3s定时器。避免页面切换效果突兀。

setTimeout(() => {

document.getElementById("main").scrollIntoView({

behavior: "smooth",

block: "start",

});

}, 300);

})

2.el-table

<el-table ref="tableRef"></el-table>
onUpdated(() => {

console.log(tableRef.value)

if(tableRef.value != null) {

tableRef.value.setScrollTop(0)

tableRef.value.setScrollLeft(0)

}

})

相关推荐
恋爱绝缘体116 小时前
CSS3 多媒体查询实例【1】
前端·css·css3
写bug的可宋16 小时前
【Electron】解决Electron使用阿里iconfont不生效问题(react+vite)
javascript·react.js·electron
小二·17 小时前
Python Web 开发进阶实战:无障碍深度集成 —— 构建真正包容的 Flask + Vue 应用
前端·python·flask
niucloud-admin1 天前
web 端前端
前端
摘星编程1 天前
React Native for OpenHarmony 实战:Linking 链接处理详解
javascript·react native·react.js
胖者是谁1 天前
EasyPlayerPro的使用方法
前端·javascript·css
EndingCoder1 天前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux35281 天前
Web集群管理实战指南:从架构到运维
运维·前端·架构
沛沛老爹1 天前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发
摘星编程1 天前
React Native for OpenHarmony 实战:ImageBackground 背景图片详解
javascript·react native·react.js