简体   繁体   English

React Swiper JS Controller

[英]React Swiper JS Controller

I have a little problem with the controller in Swiper JS.我对 Swiper JS 中的 controller 有一点问题。 I did exactly word for word as it is in the Swiper documentation, I didn't even change the names to make it work.我完全按照 Swiper 文档中的内容逐字逐句地进行操作,我什至没有更改名称以使其正常工作。 I watched their tutorial and it still doesn't work.我看了他们的教程,还是不行。 Anyone know what could be wrong?有谁知道可能出了什么问题? Thanks in advance for any answers.提前感谢您的任何答案。 Documentation: https://swiperjs.com/react文档: https://swiperjs.com/react

Btw.顺便提一句。 Instead of names that I do not want to share I put "...."而不是我不想分享的名字,我放了“....”

import React, { useState } from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperCore, {
    Autoplay,
    Mousewheel,
    Keyboard,
    Controller,
    EffectCoverflow,
    EffectFade,
} from 'swiper';
import ....
import ....
import ....

// Import Swiper styles
import 'swiper/swiper-bundle.css';

SwiperCore.use([
    Autoplay,
    Mousewheel,
    Keyboard,
    Controller,
    EffectCoverflow,
    EffectFade,
]);

const WorkSlider = () => {
    const ....Review = () => {
        return (
            <p>
                ....
            </p>
        );
    };

    const ....Review = () => {
        return (
            <p>
                ....
            </p>
        );
    };

    const ....Review = () => {
        return (
            <p>
                ....
            </p>
        );
    };

    const [firstSwiper, setFirstSwiper] = useState(null);
    const [secondSwiper, setSecondSwiper] = useState(null);
    const slides = [....Img, ....Img, ....Img];
    const reviews = [....Review, ....Review, ....Review];

    for (let i = 0; i < 3; i += 1) {
        slides.push(
            <SwiperSlide key={`slide-${i}`} tag="li">
                <img src={slides[`${i}`]} alt={`Project ${i}`}></img>
            </SwiperSlide>
        );
    }

    for (let i = 0; i < 3; i += 1) {
        reviews.push(
            <SwiperSlide key={`review-${i}`} tag="li">
                {reviews[`${i}`]}
            </SwiperSlide>
        );
    }

    return (
        <>
            <Swiper
                wrapperTag="ul"
                id="workSliderImg"
                direction="vertical"
                onSwiper={setFirstSwiper}
                controller={{ control: secondSwiper }}
                // autoplay={{ delay: 4000 }}
                mousewheel={{ enabled: true }}
                keyboard={{ enabled: true }}
                loop={{ enabled: true }}
                effect="coverflow"
                coverflowEffect={{
                    rotate: 50,
                    stretch: 0,
                    depth: 100,
                    modifier: 1,
                    slideShadows: false,
                }}
            >
                {slides}
            </Swiper>
            <Swiper
                wrapperTag="ul"
                id="workSliderReviews"
                onSwiper={setSecondSwiper}
                controller={{ control: firstSwiper }}
                direction="vertical"
                loop={{ enabled: true }}
                effect="fade"
            >
                {reviews}
            </Swiper>
        </>
    );
};

export default WorkSlider;

You're missing a prop called modules to pass the controller module您缺少一个名为 modules 的道具来传递 controller 模块

Official Documentation Example:官方文档示例:

const App = () => {
    // store controlled swiper instance
    const [controlledSwiper, setControlledSwiper] = useState(null);

    return (
      <main>
        {/* Main Swiper -> pass controlled swiper instance */}
        <Swiper modules={[Controller]} controller={{ control: controlledSwiper }} ...>
          {/* ... */}
        </Swiper>

        {/* Controlled Swiper -> store swiper instance */}
        <Swiper modules={[Controller]} onSwiper={setControlledSwiper} ...>
          {/* ... */}
        </Swiper>
      </main>
    )
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM