I'll help you implement smooth scrolling in Vue 3 using Motion.dev, which is similar to the GSAP scrollTo functionality. Here's a comprehensive example:
<template>
<div>
<nav>
<button @click="scrollToSection('section1')">Go to Section 1</button>
<button @click="scrollToSection('section2')">Go to Section 2</button>
</nav>
<div id="section1" ref="section1">
<h2>Section 1</h2>
</div>
<div id="section2" ref="section2">
<h2>Section 2</h2>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { animate } from '@motionone/solid'
const section1 = ref(null)
const section2 = ref(null)
const scrollToSection = (sectionName) => {
let target
switch(sectionName) {
case 'section1':
target = section1.value
break
case 'section2':
target = section2.value
break
}
if (target) {
animate(window, {
scrollTop: target.offsetTop,
duration: 2,
easing: 'ease-in-out'
})
}
}
</script>
<style scoped>
div {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
</style>
Key points:
- Install Motion.dev with:
npm install @motionone/solid
animate() method is similar to GSAP's .to()
- Uses
scrollTop instead of scrollTo
- Easing is slightly different from GSAP
- Uses Vue 3's
<script setup> for cleaner composition
Alternative approach using @motionone/vue:
<script setup>
import { useMotion } from '@motionone/vue'
const scrollToSection = (target) => {
useMotion(window, {
scrollTop: target.offsetTop,
transition: {
duration: 2,
easing: 'ease-in-out'
}
})
}
</script>
Differences from GSAP:
- Slightly different syntax
- Less complex animation options
- Lighter weight library
- More modern approach
Recommendation: If you need complex scrolling animations, stick with GSAP. For simpler animations, Motion.dev works great.