# Cool Horizontal Scroll in React: Build Interactions Easily

[Konstantin Lebedev](https://www.strv.com/blog/authors/konstantin)  
Frontend Engineer

---

### Time for a tutorial.

**The task? Creating a fun scroll animation in which items “flip” in the direction of the scroll**. We’re going to use [react-spring](http://react-spring.surge.sh/?ref=strv.ghost.io) for animating and [react-use-gesture](https://github.com/react-spring/react-use-gesture?ref=strv.ghost.io) to tie animation to the scroll events. The native `onScroll` event handler won't do in this case, because we'll need additional information about scrolling that native `onScroll` handler doesn't provide: scroll delta in pixels, and whether the scrolling is in progress or not.

This is what we’re going to build:

---

## BASIC SETUP

We’ll start with the basic React component you can see below. The component renders a list of images from the `public` folder, and sets them as background for `div` elements:

```jsx
const movies = [
  "/breaking-bad.webp",
  "/the-leftovers.jpg",
  "/game-of-thrones.jpg",
  "/true-detective.jpg",
  "/walking-dead.jpg"
];

const App = () => {
  return (
    <>
      <div className="container">
        {movies.map(src => (
          <div
            key={src}
            className="card"
            style={{
              backgroundImage: `url(${src})`
            }}
          />
        ))}
      </div>
    </>
  );
};
```

Next, we’ll apply some styling. We need to make sure that the container takes up 100% of the width, and that it allows its children to overflow:

```css
::-webkit-scrollbar {
  width: 0px;
}

.container {
  display: flex;
  overflow-x: scroll;
  width: 100%;
}

.card {
  flex-shrink: 0;
  width: 300px;
  height: 200px;
  border-radius: 10px;
  margin-left: 10px;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
}
```

With the basic styling, our component will look like this:

---

## ADDING ANIMATION

Let’s start by adding a rotation animation. First, we’ll replace `div` element with `animated.div`. `animated` is a decorator that extends native elements to receive animated values. Every HTML and SVG element has an `animated` counterpart that we have to use if we intend to animate that element.

Next, we’ll use `useSpring` hook from react-spring package to create a basic animation that will run when the component is mounted. Eventually, we'll bind our animation to the scroll event, but for now, it’s easier to see the result if the animation simply runs on mount.

`useSpring` hook takes an object with CSS properties that should be animated. These properties should be set to **end values** of the animation, so if we want to rotate `div`s from 0 to 25 degrees, we set the `transform` value to `rotateY(25deg)`. To set the **initial values**, we use `from` property, which itself takes an object with CSS properties.

`useSpring` hook returns a `style` object that we need to set on the target component. We see the updated code and the result below:

```jsx
import { animated, useSpring } from "react-spring";

const style = useSpring({
  from: {
    transform: "rotateY(0deg)"
  },
  transform: "rotateY(25deg)"
});

<animated.div style={style} />
```

This animation looks flat because, by default, the rotation is 2-dimensional; it’s rendered as if there was no distance between the observer and the rotation plane. `perspective` transformation allows us to move the observation point away from the rotation plane, making the 2-dimensional animation look 3-dimensional:

```jsx
const style = useSpring({
  transform: "perspective(500px) rotateY(0deg)"
});
```

(Adjust the `transform` values as needed.)

Finally, we need to add vertical padding to the container `div` to make sure that children elements aren't cut off:

```css
.container {
  width: 100%;
  padding: 20px 0;
}
```

---

## INTERACTING WITH SCROLL

Before working with scroll events, we need to adapt how we use `useSpring`. There are two important points:

- We need to be able to trigger animation manually.
- We no longer need to run animation on mount all the time.

To handle this, we’ll pass a function to `useSpring` that returns an object with CSS properties, instead of passing an object directly. It will return a tuple: the first element is the style object, the second is a `set` function to trigger animations.

We also drop the `from` property since its values will depend on the current state.

```jsx
const [style, set] = useSpring(() => ({
  transform: "perspective(500px) rotateY(0deg)"
}));
```

Now, import `useScroll` from `react-use-gesture` and bind it to the container `div`. The scroll logic:

- If `event.scrolling === true`, rotate cards by the delta value on Y-axis: `event.delta[0]`
- When scrolling stops, reset rotation to 0.

```jsx
import { useScroll } from "react-use-gesture";

const [style, set] = useSpring(() => ({
  transform: "perspective(500px) rotateY(0deg)"
}));

const bind = useScroll(event => {
  set({
    transform: `perspective(500px) rotateY(${
      event.scrolling ? event.delta[0] : 0
    }deg)`
  });
});
```

However, sharp scrolls can cause the flip to exceed 90°, which looks undesirable. To prevent excessive flipping, create a helper function that **clamps** the delta value:

```jsx
const clamp = (value, clampAt = 30) => {
  if (value > 0) {
    return value > clampAt ? clampAt : value;
  } else {
    return value < -clampAt ? -clampAt : value;
  }
};
```

Use this helper in the `useScroll`:

```jsx
const bind = useScroll(event => {
  event.scrolling ? set({ transform: `perspective(500px) rotateY(${clamp(event.delta[0])}deg)` }) : set({ transform: `perspective(500px) rotateY(0deg)` });
});
```

You can see a complete working demo of this interaction [here](https://codesandbox.io/s/react-spring-fun-scroll-vmncd?ref=strv.ghost.io).

---

### PS:
I also made the same interaction using [framer-motion](https://framer.com/motion/?ref=strv.ghost.io). The working demo is available [here](https://codesandbox.io/s/framer-motion-fun-scroll-788qb?ref=strv.ghost.io).

---

## FINAL THOUGHTS

Two decisions behind the scenes are worth mentioning:

- **Performance:** We animated only `transform` and `opacity`, properties GPU-accelerated and not taxing the main thread. It's best practice to animate only properties that benefit from GPU acceleration.
- **Responsiveness:** The horizontal scroll works well on phones and tablets. For larger screens, switching to a grid layout with media queries is advisable. The animation remains unchanged; on large screens, the horizontal scroll can be disabled, and the animation continues to work seamlessly on small screens.

---

**Don't miss anything**