Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Nakinthorn opened this issue Nov 7, 2024 · 2 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@Nakinthorn
Copy link

Nakinthorn commented Nov 7, 2024

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

@Nakinthorn Nakinthorn added the bug Something isn't working label Nov 7, 2024
Copy link

dosubot bot commented Nov 7, 2024

สวัสดีครับ @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
Copy link
Contributor

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,
          }}
        />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants