Design a Vue.js app with page transitions where the outgoing page slides left and…
- User
Design a Vue.js app with page transitions where the outgoing page slides left and incoming slides from the right. Handle scroll position - restore to top for new pages while keeping the old page's scroll locked during animation.
- Assistant
Designing Vue page transitions with scroll locking
Proposed a Vue 3 app setup using a transition wrapper around router view that applies slide-left for leaving and slide-in from right for entering pages, while locking scroll on the outgoing page by fixing body position at its scroll offset during animation and resetting scroll to top for the incoming page. The approach uses CSS absolute positioning, overflow control, and manipulating body styles to freeze scroll on old page and reset for new, ensuring smooth simultaneous transitions with preserved scroll states.Refining scroll locking with scroll wrappers and transitions
Switched from locking body scroll to confining scroll within each absolutely positioned page container inside a relative-height wrapper, enabling old page scroll position to persist naturally during slide-out by setting its overflow hidden on leave while new page starts scrolled to top. This approach simplifies scroll locking by managing scroll individually per page component, matching slide transition timings with scroll locking via CSS and lifecycle hooks for a clean simultaneous sliding experience. Below is one way to wire it up in Vue using the built‑in
<transition>around the router‑view. The key points are:- Constrain your pages to a fixed-height container and give each page its own scrollable area (
.page). - Animate the outgoing page to
translateX(-100%)and the incoming totranslateX(0)fromtranslateX(100%). - In the transition hooks, force the new page’s scroll to the top and lock the old page’s scroll by turning off its overflow while it slides away.
// main.js import { createApp } from 'vue' import App from './App.vue' import router from './router' createApp(App) .use(router) .mount('#app')// router.js import { createRouter, createWebHistory } from 'vue-router' import Home from './pages/Home.vue' import About from './pages/About.vue' const routes = [ { path: '/', name: 'home', component: Home }, { path: '/about', name: 'about', component: About } ] export default createRouter({ history: createWebHistory(), routes, // disable the built‑in scroll behavior, we manage it ourselves scrollBehavior () { return false } })<!-- App.vue --> <template> <div id="app"> <nav class="nav"> <RouterLink to="/">Home</RouterLink> <RouterLink to="/about">About</RouterLink> </nav> <div class="view"> <RouterView v-slot="{ Component, route }"> <transition name="slide" @before-enter="beforeEnter" @before-leave="beforeLeave" > <component :is="Component" :key="route.fullPath" class="page" /> </transition> </RouterView> </div> </div> </template> <script setup> /** * Ensure the new page starts at the top, and prevent scrolling of * the outgoing page while it animates away. */ const beforeEnter = (el) => { // reset scroll for the new view el.scrollTop = 0 } const beforeLeave = (el) => { // freeze the leaving view’s scroll so it stays where it was el.style.overflowY = 'hidden' } </script> <style> * { box-sizing: border-box; } html, body, #app { height: 100%; margin: 0; } .nav { display: flex; gap: 1rem; align-items: center; height: 50px; padding: 0 1rem; background: #f7f7f7; } .view { position: relative; height: calc(100% - 50px); overflow: hidden; /* hide sliding pages outside of view */ } .page { position: absolute; inset: 0; overflow-y: auto; padding: 1rem; background: #fff; -webkit-overflow-scrolling: touch; } /* slide transition */ .slide-enter-active, .slide-leave-active { transition: transform 0.35s ease; } .slide-enter-from { transform: translateX(100%); } .slide-enter-to { transform: translateX(0); } .slide-leave-from { transform: translateX(0); } .slide-leave-to { transform: translateX(-100%); } </style><!-- pages/Home.vue --> <template> <div> <h1>Home</h1> <p v-for="i in 50" :key="i">Home content line {{ i }}</p> </div> </template><!-- pages/About.vue --> <template> <div> <h1>About</h1> <p v-for="i in 50" :key="i">About content line {{ i }}</p> </div> </template>With this setup:
- The old page is frozen at its current scroll position (
overflow: hidden) during the slide‑out animation. - The new page always starts at the top (
scrollTop = 0). - The outgoing page slides to the left and the incoming page slides in from the right.
- Constrain your pages to a fixed-height container and give each page its own scrollable area (