Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locomotive Scroll v5 + Nuxt 3 Config? #560

Open
kieranmansfield opened this issue May 31, 2024 · 4 comments
Open

Locomotive Scroll v5 + Nuxt 3 Config? #560

kieranmansfield opened this issue May 31, 2024 · 4 comments

Comments

@kieranmansfield
Copy link

Hey,
What's the best way of implementing Locomotive scroll in Nuxt 3?

The general consensus is to add it as a plugin. I've tried to get it working it want to play nice with Locomotive Scroll.

Does anyone have an examples of how to get this working?

~/plugins/locomotiveScroll.client.js

import LocomotiveScroll from 'locomotive-scroll'

const locomotiveScroll = new LocomotiveScroll()

function raf(time) {
  locomotiveScroll.raf(time)
  requestAnimationFrame(raf)
}

requestAnimationFrame(raf)

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.provide('locomotiveScroll', locomotiveScroll)
})
@pieterjanmaes
Copy link

Hi,

I replied a few day's ago on an other related issue, this is how I use Locomotive scroll in Nuxt 3 => #502 (comment)

Not sure if this is the 'correct' way to implement, ...

@kieranmansfield
Copy link
Author

I tried this the other day and it didn't work for me, however I've just had another go and it worked like a treat. No idea why it was playing up before. Thank you!

@kieranmansfield
Copy link
Author

@pieterjanmaes How do I actually implement something like Scroll to top?

<script setup>
const { $lenis } = useNuxtApp()

function scrollToTop() {
  $lenis.scrollTo(0)
}
</script>

<template>
      <Button @click="scrollToTop()">Back To Top</Button>
</template>

I've added Locomotive Scroll to useNuxtApp(), it doesn't seem to do anything.

  return {
    provide: {
      locomotiveScroll: locomotiveScroll,
    },
  }

Where am I going wrong with this?

@pieterjanmaes
Copy link

@kieranmansfield, i would do it like this:

https://scroll.locomotive.ca/docs/#/attributes?id=data-scroll-to-href

Add an id to the top of your page:

<button  data-scroll-to data-scroll-to-href="#top" data-scroll-to-offset="10">
   {{ buttonText }}
</button>

Or you could create a hook in the plugin:

nuxtApp.hook('locomotive:scrolltotop', () => {
    // locomotive logic
});

And then do something like this:

const nuxtApp = useNuxtApp();
nuxtApp.callHook('locomotive:scrolltotop');

I didn't test the code, just some suggestions :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants