【问题标题】:React Carousel with text用文本反应轮播
【发布时间】:2020-04-26 17:06:04
【问题描述】:

我在我的一个反应项目中使用https://www.npmjs.com/package/react-multi-carousel,并且我已经实现了它并且它工作正常。

我现在需要在轮播上实现文本(图例),就像 https://codesandbox.io/s/lp602ljjj7 一样,它使用另一个包,但我需要那个场景而不是那个包,因为我的需求不同(使用 nextjs 所以多轮播支持ssr 并因此使用它)。

我只需要知道如何使用react-multi-carousel 在轮播图像上实现图例文本。

我的代码示例:https://codesandbox.io/s/react-multi-carousel-playground-bt3v7

【问题讨论】:

    标签: javascript reactjs carousel reactstrap react-multi-carousel


    【解决方案1】:

    将return语句的结构改为如下结构。

    然后你可以将图例的值存储在图像数组中,并将值传递给p标签

    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;
    

    【讨论】:

    【解决方案2】:

    Live demo

    你应该像下面这样改变数组结构。

    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",
        }
    ];
    

    然后在循环内添加文本并按照我们的方式设置样式!

    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>
        );
    };
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2020-04-15
    • 2021-09-20
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多