user12640294
user12640294

Reputation:

React Carousel with text

I am using https://www.npmjs.com/package/react-multi-carousel in one of my react project and I have implemented it and it works fine.

I am now in the need to implement the text (legend) over the carousel exactly like https://codesandbox.io/s/lp602ljjj7 which uses another package but I need that scenario and not that package because my need is different (Using nextjs so multi-carousel supports ssr and hence using it).

My only need is need to know how to implement the legend text over the carousel image using react-multi-carousel.

My code example: https://codesandbox.io/s/react-multi-carousel-playground-bt3v7

Upvotes: 0

Views: 9925

Answers (2)

Hardik Chaudhary
Hardik Chaudhary

Reputation: 1228

Live demo

You should change array structure like below.

const images = [
    {
        image: "https://images.unsplash.com/photo-1549989476-69a92fa57c36?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
        text: "First image",
    }
];

Then inside loop just add text and style it our way!

const Simple = ({ deviceType }) => {
    return (
        <Carousel
            ssr
            partialVisbile
            deviceType={deviceType}
            itemClass="image-item"
            responsive={responsive}
        >
            {images.map(image => {
                return (
                    <div>
                        <img
                            draggable={false}
                            alt="text"
                            style={{ width: "100%", height: "100%" }}
                            src={image.image}
                        />
                        {/* Have to style so this should see over the image */}
                        <span>{image.text}</span>
                    </div>
                );
            })}
        </Carousel>
    );
};

Upvotes: -1

Gautam Naik
Gautam Naik

Reputation: 9338

Change the structure of the return statement to the following structure.

Then you can store the value of the legend in the image array and pass the value to the p tag

const Simple = ({ deviceType }) => {
  return (
    <Carousel
      ssr
      partialVisbile
      deviceType={deviceType}
      itemClass="image-item"
      responsive={responsive}
    >
      {images.slice(0, 5).map((image, index) => {
        return (
          <div key={index} style={{ position: "relative" }}>
            <img
              draggable={false}
              alt="text"
              style={{ width: "100%", height: "100%" }}
              src={image}
            />
            <p
              style={{
                position: "absolute",
                left: "50%",
                bottom: 0,
                color: "white",
                transform: " translateX(-50%)"
              }}
            >
              Legend:{index}.
            </p>
          </div>
        );
      })}
    </Carousel>
  );
};

export default Simple;

Upvotes: 2

Related Questions