如何将jQuery轮播转换为React?

时间:2019-09-27 16:33:35

标签: jquery reactjs gatsby

因此,我尝试使用jQuery的这种灵活框轮播方法并将其转换为Gatsby的React,但我承认我对Gatsby和React都是陌生的。我不确定在这里哪里出问题了。

以下是jQuery方法的参考链接:https://codepen.io/veronicadev/pen/yjgjvL

在盖茨比有一个更简单的方法来进行轮播吗?因为我之前曾尝试安装react-carousel,但似乎不太可行。

class Products extends React.Component {
    constructor(props){
        super(props);
        this.state= {
            isActive : false,
            total : $('.carousel-item').length,
            current : 0,
        }
    }

    toggleActive = () => {
      this.state({
        isActive: !this.state.isActive,
      })
    }
    slideRight = () => {
        this.state({
            next = current,
            current = current + 1,
            setSlide(next,current) { 
                this.state({
                    slide = current,
                    if (next > total-1) = {
                      slide = 0
                      current = 0 
                    }, if (next < '0') {
                        slide = total-1,
                        current = total + 1, 
                    },
                    $('.carousel-item').onClick(toggleActive);
                })
        },
    }),
    }
    slideLeft = () => {
        this.state({
            prev = current,
            current = current - 1,
            setSlide(prev, current)

                })
            }
        })
    }

    render (){
    return (
    <div className="product-body">
        <div className="carousel">
        <div className="carousel__nav">
        <span id="moveLeft" className="carousel__arrow" onClick={this.slideLeft}>
        <svg className="carousel__icon" width="15" height="26" viewBox="0 0 15 26" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M12.8507 24.0657L2 13.215L12.8507 2.36432" stroke="white" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
            </span>
            <span id="moveRight" className="carousel__arrow" onClick={this.slideRight}>
            <svg className="carousel__icon" width="27" height="27" viewBox="0 0 27 27" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M13.4109 3.00001L24.0779 14.0312L13.0467 24.6983" stroke="white" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
            </span>
        </div>
        <div className="pages">
                    <GreenBackground
                    className={`carousel-item--01 ${isActive}`}
                    id="greenbck"
                    itemImage = {imageList.turdBurgler}
                    price="$5.00"
                    title="Turd Burgler"
                    category="stranger"
                    calories="789 calories"
                    description="Two chocolate cake donuts smothered in cookie butter glaze. Oreo chocolate chip cookie dough. With fudge drizzle."
                    />
                </div>
                </div>
        </div>
    )
                }
}
export default Products;

0 个答案:

没有答案