DEV Community

Cover image for Building a Beautiful Carousel with Left-Right Partial Visibility in React Native
Amit Kumar
Amit Kumar

Posted on

2 1 1 1 1

Building a Beautiful Carousel with Left-Right Partial Visibility in React Native

Carousels are a fantastic way to showcase images, product listings, or content in a sleek, interactive manner. In this blog, we'll build a React Native Carousel that allows users to scroll horizontally while keeping partial visibility of the next and previous images—a design commonly seen in modern UI/UX.

Image description

What We'll Build
We will use FlatList to create a horizontally scrollable carousel where:

✔ The currently selected image is centered.
✔ The previous and next images remain partially visible.
✔ The scrolling feels smooth and snaps to each image perfectly.

import {Dimensions, FlatList, Image, StyleSheet, View} from 'react-native';
import React from 'react';
import {metaData} from '../../screens/CarouselBackgroundAnimation/data';

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

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

const CarouselWithLeftRightPartialVisible = () => {
  return (
    <View style={styles.container}>
      <FlatList
        data={metaData}
        renderItem={(item, index) => <Photo item={item} index={index} />}
        horizontal
        style={{flexGrow: 0}}
        pagingEnabled
        snapToInterval={_imageWidth + _spacing}
        decelerationRate={'fast'}
        contentContainerStyle={{
          gap: _spacing,
          paddingHorizontal: (width - _imageWidth) / 2,
        }}
        showsHorizontalScrollIndicator={false}
      />
    </View>
  );
};

export default CarouselWithLeftRightPartialVisible;

const Photo = ({item, index}) => {
  return (
    <View
      style={{
        width: _imageWidth,
        height: _imageHeight,
        overflow: 'hidden',
        borderRadius: 16,
      }}>
      <Image source={{uri: item.item}} style={{flex: 1, resizeMode: 'cover'}} />
    </View>
  );
};

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

Enter fullscreen mode Exit fullscreen mode

Enhancements & Customization Ideas

Want to take it further? Here are some improvements:

🔹 Auto-scrolling: Add an interval to slide automatically.
🔹 Indicators: Show small dots to indicate the current position.
🔹 Animations: Use react-native-reanimated for smooth transitions.
🔹 Click Events: Allow users to tap and enlarge an image.

Conclusion

In this tutorial, we built a modern horizontal carousel in React Native with a partially visible left and right design. We used FlatList, snapToInterval, and simple styles to achieve a beautiful and smooth scrolling effect.

🚀 Try it in your own projects and tweak it to match your app's design!

👉 What’s next? Drop a comment if you want more tutorials on animations, gestures, or UI components in React Native. 🎨🔥

Happy coding! 🚀

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️