logo
profile picture

Chi Kin Tsang

Front-end Developer

Learn more about me.

Utrecht, The Netherlands

blog

22
Jun

Parallax Image

Recently, I've been seeing this parallax effect being used quite often on Wordpress websites. Let's recreate this subtle parallax effect using React and Motion.

Initializing the project

First, we will be creating our project and installing some packages.

  • Let's start with creating our app using NextJS by running npx create-next-app@latest in the terminal.
  • Next, for handling the animations we'll install the Motion library by running npm i motion.
  • We will be using Lenis scroll to enable smooth scroll animations by running npm i lenis.

Creating the layout

We create a list of images which we'll be using for the project.

Next we render the images from the list.

Animating the Parallax Image

First, we create a ref for the container of the image. We'll set it as the target for the useScroll hook from Motion to track its vertical progress within the viewport.

Next, we specify the offset. It describes the points in which the target intersects with the viewport.

  • "start end" means the intersection in which the top ("start") of the target element meets the bottom ("end") of the viewport. It describes the place where the element first enters the screen.
  • "end start" means the intersection in which the bottom ("end") of the target meets the top ("start") of the viewport. It's the place where the target element leaves the screen.
  • The scrollYProgress value ranges from 0 to 1 with "0" being the moment the target element first enters the viewport and "1" the moment right before the element leaves the viewport.

With the useTransform hook from Motion we can transform the scrollYProgress values into other motion values.

  • The value for imageY will be -250 pixels when the value for scrollYProgress is at "0" and 0 pixels when the value of scrollYProgress is at "1". The hook will take care of all the values in between these two numbers. This means the image will be shifted down by -250 pixels when it first enters the viewport and it will move up as the user continues to scroll down.
  • We do the opposite for textY to create a second layer for the parallax effect.

Let's put it all together and create the component.

  • To apply the animation to an element, we have to change it to a motion component. <img> turns into <motion.img> and <h1> turns into <motion.h1>. We can then access the style and customize it with our motion value.

The end result will look like this:

9
Apr

Cherry Blossom Season

The cherry blossom season is one of the most beautiful times of the year. Whoever enjoys the view of cherry blossoms can now enjoy them even locally!

Loading images...

7
Apr

Tennis elbow update

Sadly, my tennis elbow injury, which has kept me away from badminton for almost a year, has been flaring up again. It's time to put a break on doing pull-ups and dips again, until further notice. The plan is to do the exercises from this post every single day until the injury is truly gone.

5
Apr

Workout

1

1

5 reps

2

5 reps

3

5 reps

2

1

10 reps

2

10 reps

3

10 reps

27
Mar

Loading map...

is at

Hong Kong

Hong Kong

Just got back from a 2½-week vacation in Hong Kong and Beijing! It had been ages since I last visited Hong Kong, but it felt almost exactly as I remembered—a little more modern, yet still full of its quirky traditions, like yelling to the minibus driver when it's your stop and rinsing your own chopsticks and bowls before eating dim sum. One big change, though, is the modern MTR system. What once required a bus ride is now a quick metro trip, connecting major cities across the region. It's easily the best way to explore more of the country—and I definitely made the most of it!

Loading images...

9
Mar

Workout

1

1

5 reps

2

5 reps

3

5 reps

2

1

5 reps

2

5 reps

3

5 reps

8
Mar

Workout

1

1

10 reps

2

10 reps

3

10 reps

2

1

10 reps

2

10 reps

3

10 reps

3

1

10 reps

+ 6 kg

2

10 reps

+ 6 kg

3

10 reps

+ 6 kg

4

1

10 reps

+ 6 kg

2

10 reps

+ 6 kg

3

10 reps

+ 6 kg

5

1

20 reps

2

20 reps

3

20 reps

7
Mar

Workout

The sun is finally shining — the perfect time for a workout outside! Found a new pull-up and dips bar nearby too.

1

1

5 reps

2

5 reps

3

5 reps

2

1

8 reps

2

8 reps

3

8 reps

3

1

10 reps

2

10 reps

3

10 reps

Loading images...

5
Mar

Workout

1

1

10 reps

2

10 reps

3

10 reps

2

1

3 reps

2

3 reps

3

3 reps

3
Mar

Workout

1

1

8 reps

+ 6 kg

2

8 reps

+ 6 kg

3

8 reps

+ 6 kg

2

1

8 reps

+ 6 kg

2

8 reps

+ 6 kg

3

8 reps

+ 6 kg

3

1

10 reps

2

10 reps

3

10 reps