【问题标题】:Return back to first slide when carousel reaches last当轮播到达最后一张时返回第一张幻灯片
【发布时间】:2020-04-27 18:41:05
【问题描述】:

我在 react js 项目中使用https://www.npmjs.com/package/react-multi-carousel。

轮播按预期工作,但我需要让轮播在到达最后一张幻灯片时从第一个开始。

完整的工作示例: https://codesandbox.io/s/react-multi-carousel-playground-2c6ye

代码:

<Carousel
  ssr
  deviceType={deviceType}
  itemClass="image-item"
  responsive={responsive}
>

我是这样添加的,

<Carousel
  infinite={true}
  autoPlay={true}
  autoPlaySpeed={3000}
  ssr
  deviceType={deviceType}
  itemClass="image-item"
  responsive={responsive}
>

但它会自动创建无限数量的幻灯片,但这不是我的要求。一旦到达终点,它应该在 1 秒后返回到第一张幻灯片,因为用户需要向后移动 n 次数才能达到第一张幻灯片。

一旦轮播到达最后一张幻灯片,请帮助我从开始幻灯片开始(有一些延迟,例如 1000 毫秒,以便用户可以在 1 秒内看到最后一张幻灯片,然后可以查看第一张幻灯片..

【问题讨论】:

    标签: javascript css reactjs css-transitions carousel


    【解决方案1】:

    您可以通过编写您自己的自动循环和使用自定义按钮来实现这一点。老实说,也许你应该选择另一个库来做你想做的事。但是你的教育目的,我做了一个你应该做的例子。请注意,您需要为新按钮组添加 CSS。

    import React, { useEffect, useRef } from "react";
    import { render } from "react-dom";
    import Carousel from "react-multi-carousel";
    import "react-multi-carousel/lib/styles.css";
    
    const responsive = {
      desktop: {
        breakpoint: { max: 3000, min: 1024 },
        items: 1,
        paritialVisibilityGutter: 60
      },
      tablet: {
        breakpoint: { max: 1024, min: 464 },
        items: 1,
        paritialVisibilityGutter: 50
      },
      mobile: {
        breakpoint: { max: 464, min: 0 },
        items: 1,
        paritialVisibilityGutter: 30
      }
    };
    const images = [
      "https://images.unsplash.com/photo-1549989476-69a92fa57c36?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60",
      "https://images.unsplash.com/photo-1549396535-c11d5c55b9df?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60",
      "https://images.unsplash.com/photo-1550133730-695473e544be?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60"
    ];
    
      /* ADD THIS LINE */
      // Your custom Button group. CSS need to be added
    const ButtonGroup = ({ next, previous, goToSlide, ...rest }) => {
      const {
        carouselState: { currentSlide }
      } = rest;
      const lastImageIndex = images.length - 1;
      return (
        <div className="carousel-button-group" style={{ position: "absolute" }}>
          <button
            onClick={() =>
              currentSlide === 0 ? goToSlide(lastImageIndex) : previous()
            }
          >
            Prev
          </button>
          <button
            onClick={() =>
              currentSlide === lastImageIndex ? goToSlide(0) : next()
            }
          >
            Next
          </button>
        </div>
      );
    };
      /* TO THIS LINE */
    
    const Simple = ({ deviceType }) => {
      /* ADD THIS LINE */
      const carousel = useRef(null);
      const lastImageIndex = images.length - 1;
    
      useEffect(() => {
        const autoloop = setInterval(() => {
          if (carousel.state.currentSlide === lastImageIndex) {
            carousel.goToSlide(0);
          } else {
            carousel.next();
          }
        }, 3000); // Your custom auto loop delay in ms
        return () => clearInterval(autoloop);
      }, []);
      /* TO THIS LINE */
    
      return (
        <Carousel
          ssr
          deviceType={deviceType}
          itemClass="image-item"
          responsive={responsive}
          /* ADD THIS LINE */
          ref={el => (carousel = el)}
          arrows={false}
          customButtonGroup={<ButtonGroup />}
          /* TO THIS LINE */
        >
          {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>
      );
    };
    
    render(<Simple />, document.getElementById("root"));
    

    希望对您有所帮助。快乐编码:)

    【讨论】:

    • 我已经实现了你在这里给出的codesandbox.io/s/react-multi-carousel-playground-9ul7b 但它会抛出错误..你能检查一下吗?
    • 将 const carousel 更改为 let carousel。它不应该导致任何错误,但它可能是一个代码框问题......不要忘记清除你的间隔。
    • 你能帮我如何在基于类的组件中实现这一点,因为在我的实际应用程序中,我正在使用类组件,我可以看到我们不能在类组件中使用useEffect..
    • 相当于componentDidMount和ComponentWillUnmount。有关详细信息,请参阅此处的文档:reactjs.org/docs/hooks-effect.html。开始学习 hooks 对你来说真的是一件好事 :)
    • 这是错误的,你为 carousel ref 赋值是 const
    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2012-12-24
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2013-08-24
    相关资源
    最近更新 更多