All Projects → DidoMarchet → starter-kit-nuxt-locomotive-scroll

DidoMarchet / starter-kit-nuxt-locomotive-scroll

Licence: MIT license
Simple starter kit to use Locomotive Scroll and Nuxt without giving it a second though.

Programming Languages

Vue
7211 projects
javascript
184084 projects - #8 most used programming language
SCSS
7915 projects

Projects that are alternatives of or similar to starter-kit-nuxt-locomotive-scroll

Portfolio
Nuxt & Vue based new portfolio website 🚀
Stars: ✭ 24 (-75.26%)
Mutual labels:  nuxt
vue-plausible
Plausible Analytics Vue.js Plugin and NuxtJS Module
Stars: ✭ 107 (+10.31%)
Mutual labels:  nuxt
board
A complete admin board template with a large variety of elements and components, based on blexar CSS framework.
Stars: ✭ 55 (-43.3%)
Mutual labels:  nuxt
unplugin-icons
🤹 Access thousands of icons as components on-demand universally.
Stars: ✭ 2,064 (+2027.84%)
Mutual labels:  nuxt
lesspod-2.0
Serverless CMS (Currently In Development)
Stars: ✭ 60 (-38.14%)
Mutual labels:  nuxt
create-nuxt-tailwind-app
[DEPRECATED] Use create-nuxt-app, they added full tailwindcss & purgecss support
Stars: ✭ 32 (-67.01%)
Mutual labels:  nuxt
nuxt-typo3
TYPO3 Frontend rendered in Vue.js and Nuxt (frontend for EXT:headless)
Stars: ✭ 66 (-31.96%)
Mutual labels:  nuxt
loading-screen
🚥Loading screen for webpack plugin inspired by Nuxt.js's loading screen
Stars: ✭ 56 (-42.27%)
Mutual labels:  nuxt
platform
Community platform for dancers
Stars: ✭ 30 (-69.07%)
Mutual labels:  nuxt
nuxt-ts-module
A tiny module to use Typescript within Nuxt.js application.
Stars: ✭ 21 (-78.35%)
Mutual labels:  nuxt
nuxtjsbrasil.github.io
Site da comunidade Nuxt Brasil (usando Nuxtjs) 😄
Stars: ✭ 21 (-78.35%)
Mutual labels:  nuxt
nuxt-svg-sprite-module
A quick and dirty way to inline svg sprites in your Nuxt app.
Stars: ✭ 21 (-78.35%)
Mutual labels:  nuxt
bangumi
Bangumi List
Stars: ✭ 20 (-79.38%)
Mutual labels:  nuxt
nuxt-netlify-cms-starter
NuxtJS + Netlify CMS blog starter project
Stars: ✭ 35 (-63.92%)
Mutual labels:  nuxt
LaraNuxt
Laravel framework with integrated NuxtJs support, preconfigured for eslint, jest and vuetify.
Stars: ✭ 53 (-45.36%)
Mutual labels:  nuxt
nuxt-viewport
🌈 Define custom viewports for your Nuxt project
Stars: ✭ 55 (-43.3%)
Mutual labels:  nuxt
hackaru-desktop
Hackaru for desktop
Stars: ✭ 37 (-61.86%)
Mutual labels:  nuxt
lunr-module
Full-text search with pre-build indexes for Nuxt.js using lunr.js
Stars: ✭ 45 (-53.61%)
Mutual labels:  nuxt
taller-nuxt
🚀Explicación y código del taller de Nuxt para Wecodefest 2019
Stars: ✭ 20 (-79.38%)
Mutual labels:  nuxt
geofind
Multiplayer Geographical Guessing Game using PostGIS, Nuxt, Leaflet & Colyseus.
Stars: ✭ 31 (-68.04%)
Mutual labels:  nuxt

How to Locomotive Scroll and Nuxt

❤️ Every one loves smooth scrolling!

💤 But sometimes working with Javascript frameworks and DOM can be boring and love fades away.

📦 With this simple starter kit you can have fun with Locomotive Scroll and Nuxt without giving it a second though.

Table of content:

You can try this starter kit by cloning this repo and running:

# install dependencies
$ npm install

# run dev enviroment
$ npm run dev

# generate static project
$ npm run generate

Plugin

First of all we setup the plugin enabling Locomotive Scroll instance works globally both in our component and for your own purposes.

In /LocomotiveScroll/plugin/index.js we create the plugin:

import LocomotiveScroll from 'locomotive-scroll'
import 'locomotive-scroll/dist/locomotive-scroll.css'

const install = (Vue) => {
  Vue.prototype.LocomotiveScroll = LocomotiveScroll
}

export default install

if (typeof window !== 'undefined' && window.Vue) {
  window.Vue.use(install)
  if (install.installed) {
    install.installed = false
  }
}

After the setup, it will be used in /plugins/client.js.

/plugins/client.js works with mode: 'client' in the Nuxt plugins configuration .

Component

This component is an useful wrap for our Locomotive Scroll implementation.

Below are the main steps of the implementation.

Complete code can be found here /LocomotiveScroll/component/index.js.

Markup

<div
  v-locomotive="{ options }"
  class="js-locomotive"
>
  <slot />
</div>

The v-locomotive directive gets access to low-level DOM.

It takes one argument options.

options is a computed obtained merging the defaultOption data property with the gettedOptions prop.

defaultOption and gettedOptionscontain the Locomotive Scroll instance options.

computed: {
  options () {
    // this.defaultOptions = { smooth: true }
    // this.gettedOptions = { offset: ['30%',0], direction: 'horizontal' }
    return { ...this.defaultOptions, ...this.gettedOptions }
  }
}

Through the slot element we're able to pass content to the component from each page.

Directive

directives: {
  locomotive: {
    inserted (el, binding, vnode) {
      vnode.context.locomotive = new vnode.context.LocomotiveScroll({ el, ...binding.value.options })
      vnode.context.locomotive.on('scroll', (e) => {
        vnode.context.onScroll(e)
        vnode.context.$emit('scroll')
      })
      vnode.context.$emit('init')
    },
    unbind (el, binding, vnode) {
      vnode.context.locomotive.destroy()
      vnode.context.locomotive = undefined
    }
  }
}

In the inserted hook we create the new instance of Locomotive Scroll from the plugin previously created and we assign it to locomotive data property. The inserted hook guarantees the parent presence.

Once initialized we listen to scroll event.

Each time scroll event is fired we call onScroll method.

onScroll takes as parameter the scroll instance and uses this data to fill the store (/store/app.js) making the state of the scroll accessible and usable in all our application.

methods: {
  onScroll (e) {
    if (typeof this.$store._mutations['app/setScroll'] !== 'undefined') {
      this.$store.commit('app/setScroll', {
        isScrolling: this.locomotive.scroll.isScrolling,
        limit: { ...e.limit },
        ...e.scroll // x, y
      })
    }
  }
}

Implementation

Before using our component in the page we declare it globally in /plugins/both.js. /plugins/both.js is called in the Nuxt plugins configuration.

Once the plugin is global we can use it in our page or components in this way (/pages/index.vue):

<template>
  <LocomotiveScroll 
    ref="scroller" 
    :getted-options="{
      offset: ['30%',0],
      direction: 'horizontal'
      // Other options
    }">
  
    <!-- My Content:
    Html elements, Components...
    -->
    
  </LocomotiveScroll>
</template>

You can access to locomotive scroll instance using this.$refs.scroller.locomotive.

Gotchas

Reactive elements alter the state of the application and DOM's elements could change.

This changes can take place in nested components and updating Locomotive Scroll could be complex.

We can use the $nuxt helper and emit a global event

this.$nuxt.$emit('update-locomotive')

and listen it in the mounted hook in /LocomotiveScroll/component/index.vue component:

mounted () {
  this.$nuxt.$on('update-locomotive', () => {
    this?.locomotive?.update() // ?. is the Optional Chaining operator (https://www.joshwcomeau.com/operator-lookup?match=optional-chaining)
  })
}

Examples

Basic Scroll

https://starter-kit-nuxt-locomotive-scroll.netlify.app/

Horizontal Scroll

https://starter-kit-nuxt-locomotive-scroll.netlify.app/horizontal-scroll/

Gsap Scroll Trigger

https://starter-kit-nuxt-locomotive-scroll.netlify.app/scroll-trigger/

Image Loads

https://starter-kit-nuxt-locomotive-scroll.netlify.app/image-loads/

On Call Function

https://starter-kit-nuxt-locomotive-scroll.netlify.app/on-call/

Thanks

If you find this repo useful and you saved time, well... let's take a coffee together!

https://www.buymeacoffee.com/davide_marchet

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].