DEV Community

Cover image for Parallax In Next.js using React-Scroll-Parallax 😉
Ritesh Kumar
Ritesh Kumar

Posted on

Parallax In Next.js using React-Scroll-Parallax 😉

Parallax a very 🆒 looking effect which can be achieved by changing the speed of the layers in the background 🌃.

Image description

Today lets explore how we can make a similar parallax effect in nextjs using a package called react-scroll-parallax 🥰.

Image description

https://react-scroll-parallax.damnthat.tv/docs/intro



npm i react-scroll-parallax


Enter fullscreen mode Exit fullscreen mode

lets initialize our project using create-next-app



npx create-next-app --example with-tailwindcss parallax


Enter fullscreen mode Exit fullscreen mode

I made a complete tutorial on youtube you can also check it out

now lets continue our blog

First of all replace all typescript file to JavaScript as the starter template comes configured with typescript 😞 or you can also write normal JavaScript 😲 inside the typescript file not a big deal

So first wrap our Component in _app.js with ParallaxProvider and as we are creating our parallax in horizontal scrolling so we have to mention the scrollAxis='horizontal'



import '../styles/globals.css'
import { ParallaxProvider } from 'react-scroll-parallax'

function MyApp({ Component, pageProps }) {
  return (
    <ParallaxProvider scrollAxis='horizontal'>
      <Component {...pageProps} />
    </ParallaxProvider>
  )
}

export default MyApp


Enter fullscreen mode Exit fullscreen mode

the final effect that we want is something like this 👇🏻

Image description

So as there is our main scene and there are some different components like train, cloud and sun and we want to move the cloud and train with relative to our background image and we want to make sun to be static so we will use useRef and useParallax from react-scroll-parallax to achieve this we will create a const target which will store the outermost div and and then we will use useParallax to create the refs for the inner divs and pass speed and targetElement in the useParallax here is the code 👇🏻



import { useParallax } from "react-scroll-parallax";
import React, { useRef } from "react";
import Image from "next/image";

const index = () => {
  const target = useRef(null);
  const train = useParallax({
    speed: 500,
    targetElement: target.current,
  })

  const cloud = useParallax({
    speed: 200,
    targetElement: target.current,
  })

  return (
    <div ref={target} style={{
      backgroundImage: "url('/Scene.png')",
      backgroundSize: "cover",
      backgroundPosition: "center",
      width: '3000px'
    }} className="h-screen">
      <div className="fixed top-10 left-40">
        <Image src="/Sun.png" height={120} width={120} />
      </div>
      <div ref={train.ref} className="absolute"
        style={{
          top: '11vh',
          left: '30vw',
        }}
      >
        <Image src="/Train.png" height={350} width={700} />
      </div>
      <div ref={cloud.ref} className="absolute top-10">
        <Image src="/Cloud.png" height={200} width={1000} />
      </div>
    </div>
  );
}
export default index;



Enter fullscreen mode Exit fullscreen mode

complete code in github -> https://github.com/nyctonio/yt-parallax-tutorial

🥳🥳🥳🥳🎊🎊🎊🎊 Hurrayyyyy!!!! you have created a parallax effect I would recommend you to check out some of these resources for more knowledge 👇🏻

Connect me on Twitter :- Twitter 🤝🏻

Do check out my Github for amazing projects:- Github 🤝🏻

Connect me on LinkedIn :- Linkedin 🤝🏻

Read my another article :- Authentication in nodejs with mongodb bcrypt and jwt web tokens

All React Hooks in A single Post

Top comments (2)

Collapse
 
venustar1228 profile image
venustar1228 • Edited

Great. Tutorial. Thank you.
Now I am adding parallax effect to my project, but not working properly.
Can you help me, please?

Collapse
 
aptshelter profile image
aptshelter

great tutorial