javascript - 如何在 react 手势画廊制作的幻灯片的幻灯片上添加按钮或文本

标签 javascript reactjs

我使用react-gesture-galleryreact-gesture-responder制作了幻灯片。代码在这里:

    import React, { Component, useState } from "react";
    import ReactDOM from "react-dom";
    import { Gallery, GalleryImage } from "react-gesture-gallery";

    const images = [
      "https://images.unsplash.com/photo-1559666126-84f389727b9a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1356&q=80",
      "https://images.unsplash.com/photo-1557389352-e721da78ad9f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
      "https://images.unsplash.com/photo-1553969420-fb915228af51?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1049&q=80",
      "https://images.unsplash.com/photo-1550596334-7bb40a71b6bc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80",
      "https://images.unsplash.com/photo-1550640964-4775934de4af?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80"
    ];

    function App() {
      const [index, setIndex] = useState(0);

      React.useEffect(() => {
        const interval = setInterval(() => {
          if (index == images.length - 1) {
            setIndex(0);
          } else {
            setIndex(prevVal => prevVal + 1);
          }
        }, 2500);

        return () => clearInterval(interval);
      });

      return (
        <Gallery
          style={{
            height: "100vh",
            width: "100vw",
            backgroundColor: "#0a0a0a"
          }}
          index={index}
          onRequestChange={i => {
            setIndex(i);
          }}
        >
          {images.map(img => (
            <GalleryImage src={img} objectFit={"contain"} />
          ))}
        </Gallery>
      );
    }

    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);

代码可用 here

我想在每张幻灯片中添加一些 html。例如,有关该幻灯片和按钮的简短说明。

enter image description here

我是 react 新手。您是否推荐其他库来在 React 中制作幻灯片和图片库?

请问有人吗?谢谢

最佳答案

返回 map 函数内的html内容。

{images.map(image => (
  <div>
    <GalleryImage objectFit="contain" key={image} src={image} />
    <h1 className="description">Description here</h1>
  </div>
))}

如果您希望描述出现在图像顶部,请使用带有边距顶部的position:absolute

DEMO

关于javascript - 如何在 react 手势画廊制作的幻灯片的幻灯片上添加按钮或文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58824198/

相关文章:

javascript - 渲染内的三元运算符行为不正确 -react

javascript - 如何根据 javascript 值使动画持续时间动态化

javascript - 在 MeteorJS 中完成注册过程后无法获取用户详细信息

javascript - 为输入标签的值添加前缀/后缀

javascript - 如何在日期选择器中设置两天之间的类(class)?

reactjs - Antd RadioGroup onChange 在设置单选值之前触发

reactjs - 通过多个组件将 props 传递给子组件是一种不好的做法吗?

javascript - 如何在 react-router v1.0.0 中填充 this.props.params?

javascript - 从 div 中选择文本并将其包含到电子邮件中

javascript - 如何使用 cheerio 获取具有不同类的元素?