dohooo / react-native-reanimated-carousel

🎠 React Native swiper/carousel component, fully implemented using reanimated v2, support to iOS/Android/Web. (Swiper/Carousel)
https://react-native-reanimated-carousel.vercel.app
MIT License
2.85k stars 329 forks source link

How to show on the carousel a piece of the previous and next slide? #717

Open Nakinthorn opened 2 weeks ago

Nakinthorn commented 2 weeks ago

How to show on the carousel a piece of the next slide? image

dosubot[bot] commented 2 weeks ago

สวัสดีครับ @Nakinthorn! I'm here to help you with any coding bugs, answer your questions, and assist you in becoming a contributor. Let's tackle this together! If you have any questions, feel free to ask.

To display a portion of the previous and next slides on a carousel using react-native-reanimated-carousel, you can use custom animations and adjust the size and positioning of the carousel items. Here's an example:

import * as React from "react";
import { View, Text } from "react-native";
import { interpolate } from "react-native-reanimated";
import Carousel from "react-native-reanimated-carousel";
import { window } from "@/constants/sizes";

function Index() {
  const scale = 0.9;
  const RIGHT_OFFSET = window.width * (1 - scale);
  const ITEM_WIDTH = window.width * scale;
  const ITEM_HEIGHT = 120;

  const animationStyle = React.useCallback(
    (value: number) => {
      "worklet";
      const translateY = interpolate(value, [-1, 0, 1], [-ITEM_HEIGHT, 0, ITEM_HEIGHT]);
      const right = interpolate(value, [-1, -0.2, 1], [RIGHT_OFFSET / 2, RIGHT_OFFSET, RIGHT_OFFSET / 3]);
      return {
        transform: [{ translateY }],
        right,
      };
    },
    [RIGHT_OFFSET],
  );

  return (
    <View style={{ flex: 1, backgroundColor: "black" }}>
      <Carousel
        loop
        vertical
        style={{
          justifyContent: "center",
          width: window.width,
          height: window.height - 100, // Adjust height as needed
        }}
        width={ITEM_WIDTH}
        pagingEnabled={false}
        height={ITEM_HEIGHT}
        data={[...new Array(10).keys()]}
        renderItem={({ index }) => (
          <View key={index} style={{ flex: 1, padding: 10 }}>
            <View
              style={{
                alignItems: "flex-start",
                flex: 1,
                justifyContent: "space-between",
                flexDirection: "row",
                borderRadius: 20,
              }}
            >
              <Text style={{ color: "white" }}>Slide {index}</Text>
            </View>
          </View>
        )}
        customAnimation={animationStyle}
      />
    </View>
  );
}

export default Index;

This example uses the customAnimation function to define transition effects, allowing parts of the adjacent slides to be visible. Adjust ITEM_WIDTH, ITEM_HEIGHT, and RIGHT_OFFSET to control how much of the adjacent slides are shown [1][2][3].

To continue talking to Dosu, mention @dosu.

BrodaNoel commented 1 week ago

I have been using this for a long time, and it works. I only saw some issues on Tablets... Make sure to take a look

        <Carousel
          ref={ref => (this.carousel = ref)}
          mode="parallax"
          loop={false}
          scrollAnimationDuration={200}
          width={WIDTH}
          height={HEIGHT}
          data={spots}
          renderItem={SpotPreviewWrapper}
          windowSize={2}
          onSnapToItem={this.onIndexChange}
          modeConfig={{
            parallaxScrollingScale: 0.75,
            parallaxAdjacentItemScale: 0.75,
            parallaxScrollingOffset: 80,
          }}
        />