React SwiperJs 自动播放不会使 swiper 自动滑动

Yuv*_*evy 8 javascript reactjs next.js swiperjs

我在 React 中使用这个 swiper:https ://swiperjs.com/react/

我试图让它“自动播放”,但它不会自动滑动。这是我尝试过的:

// https://swiperjs.com/get-started/
import React from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles({
    SwiperStyle: {
        backgroundColor: '#f5f5f5',
        height: '58px',
        width: '100%',
    },
});

export default function TextInfoSlider(props) {
    const classes = useStyles();

    return (
        <div>

            <Swiper
                loop={true}
                autoplay={{
                    delay: 500,
                    disableOnInteraction: false
                }}
            >

                <SwiperSlide>Slide 1</SwiperSlide>
                <SwiperSlide>Slide 2</SwiperSlide>
                <SwiperSlide>Slide 3</SwiperSlide>
                <SwiperSlide>Slide 4</SwiperSlide>

            </Swiper>

            <style jsx global>{`
                    .swiper-container {
                        background-color: #f5f5f5;
                    }
           `}</style>
        </div>
    )
}
Run Code Online (Sandbox Code Playgroud)

我也试过在自动播放上只使用布尔值,但它也不起作用:

        <Swiper
            loop={true}
            autoplay={true}
            }}
        >
Run Code Online (Sandbox Code Playgroud)

jon*_*y89 22

By default Swiper React uses core version of Swiper (without any additional components). If you want to use Navitation, Pagination and other components, you have to install them first

您似乎没有安装Autoplay组件。


import SwiperCore, { Autoplay } from 'swiper';
...
SwiperCore.use([Autoplay]);
Run Code Online (Sandbox Code Playgroud)

  • 这并不能解决问题 - 然而这是正确的,因为这是需要的 (3认同)

sal*_*uhi 22

https://swiperjs.com/demos#autoplay

从 swiper 导入模块并将其作为 props 传递给 Swiper 组件

import { Swiper, SwiperSlide } from "swiper/react";
import { Autoplay, Pagination, Navigation } from "swiper";

   <Swiper
        spaceBetween={30}
        centeredSlides={true}
        autoplay={{
          delay: 2500,
          disableOnInteraction: false,
        }}
        pagination={{
          clickable: true,
        }}
        navigation={true}
        modules={[Autoplay, Pagination, Navigation]}
        className="mySwiper"
      >
        <SwiperSlide>Slide 1</SwiperSlide>
        <SwiperSlide>Slide 2</SwiperSlide>
        <SwiperSlide>Slide 3</SwiperSlide>
        <SwiperSlide>Slide 4</SwiperSlide>
        <SwiperSlide>Slide 5</SwiperSlide>
        <SwiperSlide>Slide 6</SwiperSlide>
        <SwiperSlide>Slide 7</SwiperSlide>
        <SwiperSlide>Slide 8</SwiperSlide>
        <SwiperSlide>Slide 9</SwiperSlide>
      </Swiper>
Run Code Online (Sandbox Code Playgroud)


Jon*_*nas 7

App.js在您喜欢的地方或任何地方配置 swiper :

import 'swiper/swiper-bundle.css';
import SwiperCore, { Autoplay } from 'swiper';

function App() {

  SwiperCore.use([Autoplay])

  ...
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用:

<Swiper autoplay={{ delay: 3000 }} >...</Swiper>


fat*_*emi 7

在 NextJs 中使用以下代码为我运行:

import {  Pagination } from 'swiper';
import SwiperCore, { Autoplay } from 'swiper';
import 'swiper/css';
import 'swiper/css/pagination';
import 'swiper/css/autoplay';

// ...
const HomeComponent = () => {
  SwiperCore.use([Autoplay]);
return (
    <Swiper
          className="home_slider"
          modules={[Pagination,Autoplay]}
          slidesPerView={1}
          onSlideChange={() => console.log('slide change')}
          onSwiper={(swiper) => console.log(swiper)}
          pagination={{ clickable: true }}
          autoplay
        >
          <SwiperSlide>
           <Image src={Image1}  alt='' />
          </SwiperSlide>
</Swiper>
)
Run Code Online (Sandbox Code Playgroud)


小智 6

适用于 swiper 版本 8.3

进口应该是这样的

import { Autoplay, Pagination } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/react';

import "swiper/css";
import "swiper/css/pagination";
import "swiper/css/autoplay";
Run Code Online (Sandbox Code Playgroud)

和你的刷卡反应组件,我的没有工作,autoplay={true}所以我无论如何添加autoplay={{delay: 2000}下面是我的整个刷卡它将帮助你

<Swiper
    modules={[Autoplay, Pagination]}
    pagination={{clickable: true}}
    slidesPerView={1}
    autoplay={{
          delay: 2000,
          pauseOnMouseEnter: true,
          disableOnInteraction: false
         }}
    loop
    className='swiper-container'
 >
Run Code Online (Sandbox Code Playgroud)


Chi*_*shi 5

首先你需要导入

import { Autoplay, Pagination } from "swiper";
Run Code Online (Sandbox Code Playgroud)

然后在Swiper组件中

 <Swiper
    slidesPerView={1}
    loop={true}
    pagination={{ clickable: true}}
    modules={[Pagination,Autoplay]}
    className={`mySwiper ${styledClasses}`}
    autoplay={{ delay: 2000}}
  > 
  {/* Slider here */}
  </Swiper
Run Code Online (Sandbox Code Playgroud)

这将在reactjs中正确运行它