微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使我的React滑块组件在x轴上无限大?

如何解决如何使我的React滑块组件在x轴上无限大?

我以前从未构建过React滑块,但我一直在努力使其成为无限循环的幻灯片。当前有两张幻灯片,但我想制作一张,以便用户可以单击“下一个/上一个”按钮,并且幻灯片是无限的,一遍又一遍地重复幻灯片1和幻灯片2。

我为幻灯片内容构建了一个名为Testimonial的容器。容器的宽度为800像素。

滑块具有nextprev按钮,以使滑块向左和向右移动。

Slider.tsx

import React,{ useState } from 'react';
import styled from '@emotion/styled';
import Img from 'gatsby-image';
import { SliderTypes } from '../../@types/types';
import Testimonial from '../components/Testimonial';

const Slider = (props: SliderTypes) => {

  const sliderData = [
    {
      index: 0,image: 'image',header: 'Header text',testimonial: 'Testimonial description',},{
      index: 1,];

  const [properties,setProperties] = useState(sliderData);
  const [property,setProperty] = useState(sliderData[0]);

  const nextProperty = () => {
    const newIndex = property.index + 1;
    setProperty(properties[newIndex]);
  };

  const prevProperty = () => {
    const newIndex = property.index - 1;
    setProperty(properties[newIndex]);
  };

  return (
    <>
      <button
        onClick={() => nextproperty()}
        disabled={property.index === properties.length - 1}
      >
        Next
      </button>
      <button onClick={() => prevproperty()} disabled={property.index === 0}>
        Prev
      </button>

      <CardSlider className={`card-slider active-slide-${property.index}`}>
        <CardSliderWrapper
          style={{
            transform: `translateX(-${
              property.index * (200 / properties.length)
            }%)`,}}
        >
          {sliderData.map(({ index,image,header,testimonial }) => (
            <Testimonial
              key={index}
              images={image}
              header={header}
              testimonial={testimonial}
            />
          ))}
        </CardSliderWrapper>
      </CardSlider>
    </>
  );
};

export default Slider;

我非常感谢您的见解。预先感谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。