thumbnail点击事件的antd旋转木马

发布于 2025-02-13 07:09:15 字数 2635 浏览 0 评论 0原文

嗨,我所有我都关注 code> code

    const App = () => {
      const mediaRef = useRef(null);
      const navRef = useRef(null);
      const [direction, setDirection] = useState(null);

      const onChange = (currentSlide) => {
        if (direction === "next") {
          mediaRef.current.goTo(currentSlide + 1, false);
        } else {
          mediaRef.current.goTo(currentSlide - 1, false);
        }
      };

      const handleNext = () => {
        setDirection("next");
        navRef.current.next();
      };

      const handlePrev = () => {
        setDirection("prev");
        navRef.current.prev();
      };

      const imageList = [ some images array ];
     return (
        <>
          <>
            <Carousel
              asNavFor={navRef.current}
              ref={mediaRef}
           >
              {imageList?.map((el, id) => (
                <ImageWrapper key={id}>
                  <img src={el} alt={"name"} />
                </ImageWrapper>
              ))}
            </Carousel>
            {imageList?.length > 1 && (
              <>
                <Button onClick={handlePrev}>
                  <LeftOutlined />
                </Button>
                <Button onClick={handleNext}>
                  <RightOutlined />
               </Button>
              </>
            )}
          </>

          {imageList?.length > 1 && (
            <Carousel
              slidesToShow={3}
              centerMode={true}
              asNavFor={mediaRef.current}
              ref={(carousel) => (navRef.current = carousel)}
              beforeChange={onChange}
            >
              {imageList?.map((el, id) => (
                <>
                  <divkey={id}>
                    <img src={el} alt={"name"} />
                  </div>
                </>
              ))}
            </Carousel>
          )}

          <>
            <Button onClick={handlePrev}>
              <LeftOutlined />
            </Button>
            <Button onClick={handleNext}>
              <RightOutlined />
            </Button>
          </>
        </>
      );
    };

我正在做以下,我正在服用antd旋转木马,添加另一个用于缩略图并放置自定义箭头的轮播,单击下一个和上一个按钮,它会自动更改主图像和缩略图。

现在,我试图将OnClick事件放在缩略图上,我的意思是应该单击缩略图,然后单击,应更改大图像和缩略图。我该如何实现?

Hi all I have following code

    const App = () => {
      const mediaRef = useRef(null);
      const navRef = useRef(null);
      const [direction, setDirection] = useState(null);

      const onChange = (currentSlide) => {
        if (direction === "next") {
          mediaRef.current.goTo(currentSlide + 1, false);
        } else {
          mediaRef.current.goTo(currentSlide - 1, false);
        }
      };

      const handleNext = () => {
        setDirection("next");
        navRef.current.next();
      };

      const handlePrev = () => {
        setDirection("prev");
        navRef.current.prev();
      };

      const imageList = [ some images array ];
     return (
        <>
          <>
            <Carousel
              asNavFor={navRef.current}
              ref={mediaRef}
           >
              {imageList?.map((el, id) => (
                <ImageWrapper key={id}>
                  <img src={el} alt={"name"} />
                </ImageWrapper>
              ))}
            </Carousel>
            {imageList?.length > 1 && (
              <>
                <Button onClick={handlePrev}>
                  <LeftOutlined />
                </Button>
                <Button onClick={handleNext}>
                  <RightOutlined />
               </Button>
              </>
            )}
          </>

          {imageList?.length > 1 && (
            <Carousel
              slidesToShow={3}
              centerMode={true}
              asNavFor={mediaRef.current}
              ref={(carousel) => (navRef.current = carousel)}
              beforeChange={onChange}
            >
              {imageList?.map((el, id) => (
                <>
                  <divkey={id}>
                    <img src={el} alt={"name"} />
                  </div>
                </>
              ))}
            </Carousel>
          )}

          <>
            <Button onClick={handlePrev}>
              <LeftOutlined />
            </Button>
            <Button onClick={handleNext}>
              <RightOutlined />
            </Button>
          </>
        </>
      );
    };

I am doing following, I am taking antd carousel adding another carousel for thumbnails and putting custom arrows, clicking on next and on previouse buttons it automatically change main image and thumbnail.

Now I am trying to put onClick event on thumbnails, I mean Thumbnails should be clickable and by click, the large image and thumbnail should be changed. How can I achieve that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2025-02-20 07:09:15

您可以在缩略图上使用单击ID作为参数添加一个点击处理程序,

           <ThumbnailWrapper key={id}>
            <img
              src={el}
              alt={"name"}
              onClick={() => thumbnailClicked(id)}
            />
          </ThumbnailWrapper>

然后使用定义函数中的ID进行操作:

const thumbnailClicked = (id) => {
    console.log("thumbnail clicked:",id);
    //then e.g. set parent the same as the thumbnail.
    mediaRef.current.goTo(id, false);
  };

You can add a click handler on the thumbnail with the clicked id as parameter

           <ThumbnailWrapper key={id}>
            <img
              src={el}
              alt={"name"}
              onClick={() => thumbnailClicked(id)}
            />
          </ThumbnailWrapper>

then do something with the id in the defined function:

const thumbnailClicked = (id) => {
    console.log("thumbnail clicked:",id);
    //then e.g. set parent the same as the thumbnail.
    mediaRef.current.goTo(id, false);
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文