DEV Community

Cover image for Parallax Scroll Effect in React Native with Reanimated
Amit Kumar
Amit Kumar

Posted on

4 1 1 1 1

Parallax Scroll Effect in React Native with Reanimated

Introduction

Parallax scrolling is a popular animation technique that creates an illusion of depth by making background images move slower than foreground images. In this tutorial, we will build a beautiful Parallax Scroll Effect in React Native using react-native-reanimated. This effect is great for enhancing the user experience, especially in image carousels, onboarding screens, or feature showcases.

Image description

import {Dimensions, StyleSheet, View} from 'react-native';
import React from 'react';
import {metaData} from '../../screens/CarouselBackgroundAnimation/data';
import Animated, {
  interpolate,
  useAnimatedScrollHandler,
  useAnimatedStyle,
  useSharedValue,
} from 'react-native-reanimated';

const {width} = Dimensions.get('screen');

const _imageWidth = width * 0.7;
const _imageHeight = _imageWidth * 1.76;
const _spacing = 12;

const ParallaxView = () => {
  const scrollX = useSharedValue(0);
  const onScroll = useAnimatedScrollHandler(e => {
    scrollX.value = e.contentOffset.x / (_imageWidth + _spacing);
  });
  return (
    <View style={styles.container}>
      <Animated.FlatList
        data={metaData}
        renderItem={({item, index}) => (
          <Photo item={item} index={index} scrollX={scrollX} />
        )}
        horizontal
        style={{flexGrow: 0}}
        pagingEnabled
        snapToInterval={_imageWidth + _spacing}
        decelerationRate={'fast'}
        contentContainerStyle={{
          gap: _spacing,
          paddingHorizontal: (width - _imageWidth) / 2,
        }}
        showsHorizontalScrollIndicator={false}
        onScroll={onScroll}
        scrollEventThrottle={1000 / 60}
      />
    </View>
  );
};

export default ParallaxView;

const Photo = ({item, index, scrollX}) => {
  const styleZ = useAnimatedStyle(() => {
    return {
      transform: [
        {
          scale: interpolate(
            scrollX.value,
            [index - 1, index, index + 1],
            [1.4, 1, 1.4],
          ),
        },
        {
          rotate: `${interpolate(
            scrollX.value,
            [index - 1, index, index + 1],
            [5, 0, 5],
          )}deg`,
        },
      ],
    };
  });
  return (
    <View
      style={{
        width: _imageWidth,
        height: _imageHeight,
        overflow: 'hidden',
        borderRadius: 16,
      }}>
      <Animated.Image source={{uri: item}} style={[{flex: 1}, styleZ]} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we built a Parallax Scroll Effect in React Native using react-native-reanimated. This technique enhances the visual appeal of image carousels, making the user experience more immersive.

Key Takeaways:

  • Used useSharedValue to track the scroll position.
  • Created an animated transformation using interpolate.
  • Applied smooth scaling and rotation effects.

This is just the beginning! You can further customize this effect by adding more animations like opacity changes, blur effects, or background parallax layers.

Happy coding! 🚀

A developer toolkit for building lightning-fast dashboards into SaaS apps

A developer toolkit for building lightning-fast dashboards into SaaS apps

Embed in minutes, load in milliseconds, extend infinitely. Import any chart, connect to any database, embed anywhere. Scale elegantly, monitor effortlessly, CI/CD & version control.

Get early access

Top comments (0)

What is the Role of Obfuscation in Mobile App Protection?

What is the Role of Obfuscation in Mobile App Protection?

Attackers try to reverse engineer and tamper with apps by injecting malicious code to bypass features or steal user information. Obfuscation prevents reverse engineering by making it difficult for attackers to understand how an app functions.

Read more

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay