javascript - 当轮播到达最后一张时返回第一张幻灯片

标签 javascript css reactjs css-transitions carousel

我正在使用 https://www.npmjs.com/package/react-multi-carousel在 react js 元素中。

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

完整的工作示例: 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 秒,然后可以查看第一张幻灯片..

最佳答案

你可以通过写 来实现这一点。您自己的自动循环 并通过使用 自定义按钮 .老实说,也许您应该选择另一个可以满足您需求的库。但是你的教育目的,我做了一个你应该做的例子。请注意,您需要为新按钮组添加 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"));

希望能帮助到你。快乐编码:)

关于javascript - 当轮播到达最后一张时返回第一张幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59676168/

相关文章:

javascript - 如何用javascript读取href的内容?

javascript - 使 CSS3 过渡以恒定的速度进行

javascript - 将 hoist-non-react-statics 与 withRouter 一起使用

reactjs - 通过 ref 设置焦点仅适用于 React 中的 setTimeout?

javascript - Vimeo API 方法中的变量 `undefined`

javascript - Sails.js:在我想发送实际响应之前发送了 200 响应

css - 今天的 Flexbox - 我该如何使用它

javascript - 对象解构为命名对象

c# - HiddenField 始终为 NULL,来自 javascript

php - 如何从 SVG 制作透明的下拉按钮?