Make a effect like on the IOS version when navigate to another pages

Is it possible in NuxtJS 3 to give the effect navigation like in the mobile version? So when move to another page, it has some effect sliding.
29 Replies
kapa.ai
kapa.ai5d ago
Hi @Adnan Erlansyah. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, it's possible to create a sliding effect when navigating between pages in Nuxt 3. You can achieve this by using page transitions, which are supported natively in Nuxt 3. Here's how you can implement a sliding effect: 1. First, enable page transitions in your nuxt.config.ts file:
export default defineNuxtConfig({
app: {
pageTransition: { name: 'page', mode: 'out-in' }
},
})
export default defineNuxtConfig({
app: {
pageTransition: { name: 'page', mode: 'out-in' }
},
})
2. Then, add the following CSS to your app.vue file:
<template>
<NuxtPage />
</template>

<style>
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from,
.page-leave-to {
opacity: 0;
filter: blur(1rem);
}
</style>
<template>
<NuxtPage />
</template>

<style>
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from,
.page-leave-to {
opacity: 0;
filter: blur(1rem);
}
</style>
This will create a basic transition effect. To make it more like a sliding effect, you can modify the CSS like this:
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-leave-to {
opacity: 0;
transform: translateX(-50px);
}
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-leave-to {
opacity: 0;
transform: translateX(-50px);
}
This will create a sliding effect where the new page slides in from the right and the old page slides out to the left. You can customize this further to match the iOS-like effect you're looking for by adjusting the transition properties. For more detailed information on page transitions in Nuxt 3, you can refer to the Nuxt 3 Transitions documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5d ago
<@1079709612078534676> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Adnan Erlansyah
Adnan ErlansyahOP5d ago
@kapa.ai I think it's not affecting anything, the navigation is still same as before.
kapa.ai
kapa.ai5d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I apologize for the confusion. If the transitions are not working as expected, there could be a few reasons for this. Let's troubleshoot the issue: 1. First, make sure you have wrapped your <NuxtPage /> component in a <NuxtLayout> component in your app.vue file. According to the Nuxt 3 Transitions documentation, your app.vue should look like this:
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
2. If you're using layouts, you might need to add a layoutTransition in addition to the pageTransition in your nuxt.config.ts:
export default defineNuxtConfig({
app: {
pageTransition: { name: 'page', mode: 'out-in' },
layoutTransition: { name: 'layout', mode: 'out-in' }
}
})
export default defineNuxtConfig({
app: {
pageTransition: { name: 'page', mode: 'out-in' },
layoutTransition: { name: 'layout', mode: 'out-in' }
}
})
3. Make sure your CSS is correctly applied. The CSS should be in a <style> tag that is not scoped. If you're using a separate CSS file, ensure it's properly imported. 4. If you're still not seeing the effect, there might be an issue with the root element of your pages. According to a GitHub issue, pages must have a single root element for transitions to work properly. Ensure each of your page components has a single root element, like this:
<template>
<div>
<h1>Page Title</h1>
<p>Page content</p>
</div>
</template>
<template>
<div>
<h1>Page Title</h1>
<p>Page content</p>
</div>
</template>
5. If none of the above solutions work, you might want to try using the onBeforeEnter, onEnter, and onAfterEnter hooks for more control over the transition. Here's an example from the Nuxt 3 Transitions documentation:
<script setup lang="ts">
definePageMeta({
pageTransition: {
name: 'custom-flip',
mode: 'out-in',
onBeforeEnter: (el) => {
console.log('Before enter...')
},
onEnter: (el, done) => {
console.log('Enter...')
done()
},
onAfterEnter: (el) => {
console.log('After enter...')
}
}
})
</script>
<script setup lang="ts">
definePageMeta({
pageTransition: {
name: 'custom-flip',
mode: 'out-in',
onBeforeEnter: (el) => {
console.log('Before enter...')
},
onEnter: (el, done) => {
console.log('Enter...')
done()
},
onAfterEnter: (el) => {
console.log('After enter...')
}
}
})
</script>
If you're still experiencing issues after trying these solutions, it might be helpful to create a minimal reproduction of the problem to identify the exact cause. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5d ago
<@1079709612078534676> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Mittchel
Mittchel5d ago
<template>
<NuxtPage>
<template #page="{ Component }">
<Transition name="slide" mode="out-in">
<Component :key="$route.fullPath" />
</Transition>
</template>
</NuxtPage>
</template>

<style scoped>
.slide-enter-active,
.slide-leave-active {
transition: transform 0.3s ease-in-out; /* Adjust duration and easing */
}

.slide-enter-from {
transform: translateX(100%); /* Slide from right */
}

.slide-leave-to {
transform: translateX(-100%); /* Slide to left */
}
</style>
<template>
<NuxtPage>
<template #page="{ Component }">
<Transition name="slide" mode="out-in">
<Component :key="$route.fullPath" />
</Transition>
</template>
</NuxtPage>
</template>

<style scoped>
.slide-enter-active,
.slide-leave-active {
transition: transform 0.3s ease-in-out; /* Adjust duration and easing */
}

.slide-enter-from {
transform: translateX(100%); /* Slide from right */
}

.slide-leave-to {
transform: translateX(-100%); /* Slide to left */
}
</style>
Mittchel
Mittchel5d ago
Or using an animation library https://www.youtube.com/watch?v=37EFXLA-6dc
Vue Mastery
YouTube
How to animate Vue with GSAP
J.D. Hillen presents “Animating Vue with GSAP” at VueConf US 2022. For more vue js animations tutorials, check out our courses 👉 https://www.vuemastery.com/courses Ready to master Vue.js? With weekly Vue js tutorials on the latest topics, and exclusive content with Evan You (the creator of Vue), Vue Mastery is the ultimate learning resource for...
Adnan Erlansyah
Adnan ErlansyahOP5d ago
It's still not affecting sir.
Adnan Erlansyah
Adnan ErlansyahOP5d ago
Jam
Jam | Console logs, network requests, and more
Click this link to see the video on localhost:3000, with console logs, network tab and device information.
Mittchel
Mittchel5d ago
what is in console log That work strange but work, need to play with animations
<Body>
<v-layout>
<transition
name="slide"
mode="in-out"
>
<v-app :key="$route.fullPath">

<nuxt-page />
</nuxt-layout>
</v-app>
</transition>
</v-layout>
</Body>
<Body>
<v-layout>
<transition
name="slide"
mode="in-out"
>
<v-app :key="$route.fullPath">

<nuxt-page />
</nuxt-layout>
</v-app>
</transition>
</v-layout>
</Body>
<style scoped>
.slide-enter-active,
.slide-leave-active {
transition: transform 0.3s ease-in-out; /* Adjust duration and easing */
}

.slide-enter-from {
transform: translateX(100%); /* Slide from right */
}

.slide-leave-to {
transform: translateX(-100%); /* Slide to left */
}
</style>
<style scoped>
.slide-enter-active,
.slide-leave-active {
transition: transform 0.3s ease-in-out; /* Adjust duration and easing */
}

.slide-enter-from {
transform: translateX(100%); /* Slide from right */
}

.slide-leave-to {
transform: translateX(-100%); /* Slide to left */
}
</style>
That is more nice
<transition
name="slide-left"
mode="out-in"
>
<transition
name="slide-left"
mode="out-in"
>
<style>
.page {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
}

/* Slide left (push) */
.slide-left-enter-active, .slide-left-leave-active {
transition: transform 0.3s ease;
}
.slide-left-enter, .slide-left-leave-to /* .slide-left-leave-active in <2.1.8 */ {
transform: translateX(100%);
}
.slide-left-leave-active {
transform: translateX(-100%);
}

/* Slide right (pop) */
.slide-right-enter-active, .slide-right-leave-active {
transition: transform 0.3s ease;
}
.slide-right-enter, .slide-right-leave-to /* .slide-right-leave-active in <2.1.8 */ {
transform: translateX(-100%);
}
.slide-right-leave-active {
transform: translateX(100%);
}
</style>
<style>
.page {
position: absolute;
width: 100%;
height: 100%;
overflow: hidden;
}

/* Slide left (push) */
.slide-left-enter-active, .slide-left-leave-active {
transition: transform 0.3s ease;
}
.slide-left-enter, .slide-left-leave-to /* .slide-left-leave-active in <2.1.8 */ {
transform: translateX(100%);
}
.slide-left-leave-active {
transform: translateX(-100%);
}

/* Slide right (pop) */
.slide-right-enter-active, .slide-right-leave-active {
transition: transform 0.3s ease;
}
.slide-right-enter, .slide-right-leave-to /* .slide-right-leave-active in <2.1.8 */ {
transform: translateX(-100%);
}
.slide-right-leave-active {
transform: translateX(100%);
}
</style>
Adnan Erlansyah
Adnan ErlansyahOP4d ago
There's no anything in the console.log
No description
Adnan Erlansyah
Adnan ErlansyahOP4d ago
Maybe you can give an example from your code or implementation sir if you don't mind 🙏 Uppp
Mittchel
Mittchel4d ago
4 errors, 38 warnings, what is that, you filter it? ahaha? lol, show me error tab not info
Adnan Erlansyah
Adnan ErlansyahOP4d ago
No, I'm not filtering it.
No description
Adnan Erlansyah
Adnan ErlansyahOP4d ago
No description
Mittchel
Mittchel4d ago
129 warnings :), click to show all now try transitons on new project without formkit and other, or correct the problems with warnings you has preserve erros on in console? Make a demo on github, I'm bored, let me take a look.
Adnan Erlansyah
Adnan ErlansyahOP4d ago
Okey, I will try to make a new project for the sample.
Adnan Erlansyah
Adnan ErlansyahOP4d ago
Sir, when I try it in the new project, it's working. I also have already fixed several warnings but it's still not applied. And the warning for the cookie I think it's not affect anything right.
No description
Mittchel
Mittchel4d ago
Now add to your new project package by package, page by page, and components from old and see where it will stop working if you don't found problem. Clear cache at least 🙂
Adnan Erlansyah
Adnan ErlansyahOP4d ago
Sorry sir, it's working now 😅. I've made it like this.
<template>
<!-- <PartialsLoading v-if="isLoading" /> -->
<PartialsSplashScreen v-if="showSplash" />
<NuxtLayout>
<NuxtLoadingIndicator :throttle="0" />
<NuxtPage />
</NuxtLayout>
</template>

<script setup>

</script>

<style>
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-leave-to {
opacity: 0;
transform: translateX(-50px);
}
</style>
<template>
<!-- <PartialsLoading v-if="isLoading" /> -->
<PartialsSplashScreen v-if="showSplash" />
<NuxtLayout>
<NuxtLoadingIndicator :throttle="0" />
<NuxtPage />
</NuxtLayout>
</template>

<script setup>

</script>

<style>
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-leave-to {
opacity: 0;
transform: translateX(-50px);
}
</style>
I don't know what's happening. Now, how about I make an effect when it's go back to the previous page then the effect animation will use fade-right instead of fade-left. Is it possible? Yeah I think this is the problem 😅🙏. @kapa.ai how about I make an effect when it's go back to the previous page then the effect animation will use fade-right instead of fade-left.
kapa.ai
kapa.ai4d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To create different transition effects for navigating forward and backward between pages in Nuxt 3, you can use dynamic transitions. This approach allows you to apply a fade-right effect when going back to the previous page and a fade-left effect when moving forward. Here's how you can implement this: 1. First, make sure you have the basic transition setup in your nuxt.config.ts:
export default defineNuxtConfig({
app: {
pageTransition: { name: 'page', mode: 'out-in' }
}
})
export default defineNuxtConfig({
app: {
pageTransition: { name: 'page', mode: 'out-in' }
}
})
2. Then, you can use inline middleware to dynamically set the transition based on the navigation direction. Here's an example of how to implement this in your page component:
<script setup lang="ts">
definePageMeta({
pageTransition: {
name: 'slide-right',
mode: 'out-in'
},
middleware (to, from) {
to.meta.pageTransition = to.meta.pageTransition || {}
to.meta.pageTransition.name = +to.params.id > +from.params.id ? 'slide-left' : 'slide-right'
}
})
</script>

<template>
<div>
<h1>Page Content</h1>
</div>
</template>

<style>
.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active {
transition: all 0.2s;
}
.slide-left-enter-from {
opacity: 0;
transform: translate(50px, 0);
}
.slide-left-leave-to {
opacity: 0;
transform: translate(-50px, 0);
}
.slide-right-enter-from {
opacity: 0;
transform: translate(-50px, 0);
}
.slide-right-leave-to {
opacity: 0;
transform: translate(50px, 0);
}
</style>
<script setup lang="ts">
definePageMeta({
pageTransition: {
name: 'slide-right',
mode: 'out-in'
},
middleware (to, from) {
to.meta.pageTransition = to.meta.pageTransition || {}
to.meta.pageTransition.name = +to.params.id > +from.params.id ? 'slide-left' : 'slide-right'
}
})
</script>

<template>
<div>
<h1>Page Content</h1>
</div>
</template>

<style>
.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active {
transition: all 0.2s;
}
.slide-left-enter-from {
opacity: 0;
transform: translate(50px, 0);
}
.slide-left-leave-to {
opacity: 0;
transform: translate(-50px, 0);
}
.slide-right-enter-from {
opacity: 0;
transform: translate(-50px, 0);
}
.slide-right-leave-to {
opacity: 0;
transform: translate(50px, 0);
}
</style>
This code sets up two transitions: slide-left for moving forward and slide-right for going back. The middleware function compares the id parameter of the current and previous routes to determine the direction of navigation and sets the appropriate transition name. You can adjust the CSS to create the specific fade effects you want. For example, to create a fade effect instead of a slide, you could modify the CSS like this:
.slide-left-enter-from,
.slide-right-enter-from {
opacity: 0;
}
.slide-left-leave-to {
opacity: 0;
transform: translateX(-30px);
}
.slide-right-leave-to {
opacity: 0;
transform: translateX(30px);
}
.slide-left-enter-from,
.slide-right-enter-from {
opacity: 0;
}
.slide-left-leave-to {
opacity: 0;
transform: translateX(-30px);
}
.slide-right-leave-to {
opacity: 0;
transform: translateX(30px);
}
This will create a fade effect with a slight movement to indicate the direction. Remember to adjust the logic in the middleware function according to how you determine forward and backward navigation in your app. The example above uses route parameters, but you might need a different approach depending on your routing structure. For more detailed information and examples, you can refer to the Nuxt 3 Transitions documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@1079709612078534676> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Adnan Erlansyah
Adnan ErlansyahOP4d ago
@kapa.ai please just update my code. This is the code in my app.vue
<template>
<!-- <PartialsLoading v-if="isLoading" /> -->
<PartialsSplashScreen v-if="showSplash" />
<NuxtLayout>
<NuxtLoadingIndicator :throttle="0" />
<NuxtPage />
</NuxtLayout>
</template>

<script setup>
</script>

<style>
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-leave-to {
opacity: 0;
transform: translateX(-50px);
}
</style>
<template>
<!-- <PartialsLoading v-if="isLoading" /> -->
<PartialsSplashScreen v-if="showSplash" />
<NuxtLayout>
<NuxtLoadingIndicator :throttle="0" />
<NuxtPage />
</NuxtLayout>
</template>

<script setup>
</script>

<style>
.page-enter-active,
.page-leave-active {
transition: all 0.4s;
}
.page-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-leave-to {
opacity: 0;
transform: translateX(-50px);
}
</style>
kapa.ai
kapa.ai4d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on your current app.vue code and your request to implement different effects for forward and backward navigation, I can suggest the following updates to your code:
<template>
<PartialsSplashScreen v-if="showSplash" />
<NuxtLayout>
<NuxtLoadingIndicator :throttle="0" />
<NuxtPage />
</NuxtLayout>
</template>

<script setup>
const route = useRoute()
const router = useRouter()

let lastPath = ref(route.path)

router.afterEach((to, from) => {
to.meta.transition = to.path > lastPath.value ? 'page-forward' : 'page-backward'
lastPath.value = to.path
})
</script>

<style>
.page-forward-enter-active,
.page-forward-leave-active,
.page-backward-enter-active,
.page-backward-leave-active {
transition: all 0.4s;
}
.page-forward-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-forward-leave-to {
opacity: 0;
transform: translateX(-50px);
}
.page-backward-enter-from {
opacity: 0;
transform: translateX(-50px);
}
.page-backward-leave-to {
opacity: 0;
transform: translateX(50px);
}
</style>
<template>
<PartialsSplashScreen v-if="showSplash" />
<NuxtLayout>
<NuxtLoadingIndicator :throttle="0" />
<NuxtPage />
</NuxtLayout>
</template>

<script setup>
const route = useRoute()
const router = useRouter()

let lastPath = ref(route.path)

router.afterEach((to, from) => {
to.meta.transition = to.path > lastPath.value ? 'page-forward' : 'page-backward'
lastPath.value = to.path
})
</script>

<style>
.page-forward-enter-active,
.page-forward-leave-active,
.page-backward-enter-active,
.page-backward-leave-active {
transition: all 0.4s;
}
.page-forward-enter-from {
opacity: 0;
transform: translateX(50px);
}
.page-forward-leave-to {
opacity: 0;
transform: translateX(-50px);
}
.page-backward-enter-from {
opacity: 0;
transform: translateX(-50px);
}
.page-backward-leave-to {
opacity: 0;
transform: translateX(50px);
}
</style>
Here's what changed: 1. I added a <script setup> section to handle the dynamic transition logic. 2. We use the useRoute() and useRouter() composables to access the current route and router instance as described in the Nuxt 3 documentation. 3. We keep track of the last path visited using a ref. 4. We use router.afterEach() to set the transition name based on whether we're moving forward or backward in the navigation. This is done by comparing the current path with the last path. 5. In the <style> section, I've updated the CSS classes to match our new dynamic transition names: page-forward and page-backward. 6. The CSS for page-forward remains the same as your original page transition. 7. For page-backward, I've reversed the transform directions to create the effect of sliding right when going back. This setup should give you the desired effect: when navigating forward, the new page will slide in from the right, and when going back, it will slide in from the left. Remember to adjust the logic in router.afterEach() if you need a different way to determine forward and backward navigation. The current implementation assumes that a "higher" path (alphabetically) means moving forward, which may not always be the case depending on your routing structure. Nuxt 3 Transitions documentation provides more information on dynamic transitions if you need to further customize this behavior. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@1079709612078534676> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Adnan Erlansyah
Adnan ErlansyahOP4d ago
@kapa.ai how to redirect back to previous page?
kapa.ai
kapa.ai4d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To redirect back to the previous page in Nuxt 3, you can use the navigateTo function or the router.back() method. Here are two ways to achieve this: 1. Using navigateTo:
const router = useRouter();
navigateTo(-1);
const router = useRouter();
navigateTo(-1);
2. Using router.back():
const router = useRouter();
router.back();
const router = useRouter();
router.back();
Both methods will take the user back to the previous page in their browsing history. You can use either of these approaches in your component or page script. Here's an example of how you might use this in a component:
<template>
<button @click="goBack">Go Back</button>
</template>

<script setup>
const router = useRouter();

const goBack = () => {
navigateTo(-1);
// or
// router.back();
};
</script>
<template>
<button @click="goBack">Go Back</button>
</template>

<script setup>
const router = useRouter();

const goBack = () => {
navigateTo(-1);
// or
// router.back();
};
</script>
The navigateTo function is a Nuxt-specific utility that provides a more consistent API across server and client navigation. It's generally recommended to use navigateTo in Nuxt applications. For more information on navigation in Nuxt 3, you can refer to the Nuxt 3 documentation on programmatic navigation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@1079709612078534676> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?