Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 8, 2022 07:16 am GMT

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

lets initialize our project using create-next-app

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

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

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;

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


Original Link: https://dev.to/nyctonio/parallax-in-nextjs-using-react-scroll-parallax-2110

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To