SwiperJs轮播功能出现问题。

huangapple go评论63阅读模式
英文:

SwiperJs loop is broken

问题

我尝试使用它进行循环,但当我的每个视图有2张幻灯片时,我无法在最后一张卡片中向右方向循环回到第一张卡片元素,但如果我向左方向前进,很容易实现循环。

我所做的更改在"space-between"部分。

代码:

import React from "react";

import "./testimonial.css";
import { Data } from './Data';

// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";

// Import Swiper styles
import "swiper/css";
import "swiper/css/pagination";

// import required modules
import { Pagination } from "swiper";

const Testimonials = () => {
  return (
    <section className="testimonial container section">
      <h2 className="section__title">Testimonials</h2>
      <span className="section__subtitle">What my Clients say</span>

      <Swiper className="testimonial__container"
        loop={true}
        grabCursor={true}
        spaceBetween={24}
        pagination={{
          clickable: true,
        }}
        breakpoints={{
          576: {
            slidesPerView: 2,
          },
          768: {
            slidesPerView: 2,
            spaceBetween: 48,
          }
        }}
        modules={[Pagination]}
      >
        {Data.map(({id, image, title, description}) => {
          return (
            <SwiperSlide className="testimonial__card" key={id}>
              <img src={image} alt={title} className="testimonial__img" />
              <h3 className="testimonial__name">{title}</h3>
              <p className="testimonial__description">{description}</p>
            </SwiperSlide>
          )
        }}
      </Swiper>
    </section>
  )
}

export default Testimonials

我期望在两个方向上都获得完整的循环行为,但只在一个方向上获得了循环行为,即从右到左。

英文:

I tried to use it for looping but when my slides per view are 2 I cant loop back to first card element in right direction on last card, while I can easily loop if I go in left direction

Changes I have made are in space-between

CODE:

import React from &quot;react&quot;;
import &quot;./testimonial.css&quot;;
import { Data } from &#39;./Data&#39;;
// Import Swiper React components
import { Swiper, SwiperSlide } from &quot;swiper/react&quot;;
// Import Swiper styles
import &quot;swiper/css&quot;;
import &quot;swiper/css/pagination&quot;;
// import required modules
import { Pagination } from &quot;swiper&quot;;
const Testimonials = () =&gt; {
return (
&lt;section className=&quot;testimonial container section&quot;&gt;
&lt;h2 className=&quot;section__title&quot;&gt;Testimonials&lt;/h2&gt;
&lt;span className=&quot;section__subtitle&quot;&gt;What my Clients say&lt;/span&gt;
&lt;Swiper className=&quot;testimonial__container&quot;
loop={true}
grabCursor={true}
spaceBetween={24}
pagination={{
clickable: true,
}}
breakpoints={{
576: {
slidesPerView: 2,
},
768: {
slidesPerView: 2,
spaceBetween: 48,
}
}}
modules={[Pagination]}
&gt;
{Data.map(({id, image, title, description})=&gt;{
return (
&lt;SwiperSlide className=&quot;testimonial__card&quot; key={id}&gt;
&lt;img src={image} alt={title} className=&quot;testimonial__img&quot; /&gt;
&lt;h3 className=&quot;testimonial__name&quot;&gt;{title}&lt;/h3&gt;
&lt;p className=&quot;testimonial__description&quot;&gt;{description}&lt;/p&gt;
&lt;/SwiperSlide&gt;
)
})}
&lt;/Swiper&gt;
&lt;/section&gt;
)
}
export default Testimonials

I was expecting a complete loop behaviour in both directions but got loop behaviour in only 1 direction ie right to left

答案1

得分: 0

我遇到了相同的问题!但我找到了解决方案,如果你使用 Swiper v9,循环可能无法双向工作,在我的情况下,只有当我继续使用 PrevSlides 时它才能正常工作。所以我做的是降级到 v8,然后实现了循环,它开始在双向中工作。

这是我使用的版本:

"swiper": "8.0.0",

这是我的带循环的 Swiper 实现。我在这里使用了自定义导航按钮,如果你不想要它们,只需删除它们并将 navigation={false} 更改为 navigation={true}。

import React, { useState } from "react";
import Image from "next/image";
import { Swiper, SwiperSlide } from "swiper/react";
import { FreeMode, Navigation, Thumbs } from "swiper";

import coach1 from "public/images/coaches/coach1.jpeg";
import coach2 from "public/images/coaches/coach2.jpeg";
import arrowLeft from "public/icons/arrow-left.svg";
import CoachCard from "./CoachCard";

import "swiper/css";
import "swiper/css/navigation";
import "swiper/css/free-mode";

const Slider = () => {
  const [swiper, setSwiper] = useState<any>(null);

  const coachData = [
    {
      id: 1,
      name: "MILEY CYRUS",
      categories: ["HIIT", "Strength Training", "Strength Training"],
      image: coach1,
    },
    {
      id: 2,
      name: "BROOKLYN MOORE",
      categories: ["Pilates", "Yoga"],
      image: coach2,
    },
    {
      id: 3,
      name: "MILEY CYRUS",
      categories: ["HIIT", "Strength Training", "Strength Training"],
      image: coach1,
    },
    {
      id: 4,
      name: "BROOKLYN MOORE",
      categories: ["Pilates", "Yoga"],
      image: coach2,
    },
  ];

  const handleSwiperInit = (swiper: any) => {
    setSwiper(swiper);
  };

  return (
    <div>
      <Swiper
        loop={true}
        spaceBetween={10}
        slidesPerView={2.2}
        modules={[FreeMode, Navigation, Thumbs]}
        className="mySwiper2"
        navigation={false}
        onInit={handleSwiperInit}
      >
        {coachData.map((item, index) => (
          <SwiperSlide key={index}>
            <CoachCard coach={item} />
          </SwiperSlide>
        ))}
      </Swiper>

      <div className="mt-[50px] flex justify-center gap-4">
        <span
          className="cursor-pointer"
          onClick={() => {
            swiper?.slidePrev();
          }}
        >
          <Image src={arrowLeft} alt="arrowLeft" />
        </span>

        <span
          className="cursor-pointer"
          onClick={() => {
            swiper?.slideNext();
          }}
        >
          <Image src={arrowLeft} alt="arrowLeft" className="rotate-180" />
        </span>
      </div>
    </div>
  );
};

export default Slider;

希望这可以帮助你解决问题!

英文:

I ran into the same Problem! But i found the solution that was if you are using swiper v9 then looping might not work in both ways in my case it was only working if I keep going to PrevSlides. So what i did was downgraded to v8 and then implemented the looping and it started working in both Directions.

Here is the version that i'm using

&quot;swiper&quot;: &quot;8.0.0&quot;,

Here is my implementation for the Swiper with Looping. I have used here custom navigation buttons if you don't want that you can just remove them and change navigation={false} to navigation={true}

 import React, { useState } from &quot;react&quot;;
import Image from &quot;next/image&quot;;
import { Swiper, SwiperSlide } from &quot;swiper/react&quot;;
import { FreeMode, Navigation, Thumbs } from &quot;swiper&quot;;
import coach1 from &quot;public/images/coaches/coach1.jpeg&quot;;
import coach2 from &quot;public/images/coaches/coach2.jpeg&quot;;
import arrowLeft from &quot;public/icons/arrow-left.svg&quot;;
import CoachCard from &quot;./CoachCard&quot;;
import &quot;swiper/css&quot;;
import &quot;swiper/css/navigation&quot;;
import &quot;swiper/css/free-mode&quot;;
const Slider = () =&gt; {
const [swiper, setSwiper] = useState&lt;any&gt;(null);
const coachData = [
{
id: 1,
name: &quot;MILEY CYRUS&quot;,
categories: [&quot;HIIT&quot;, &quot;Strength Training&quot;, &quot;Strength Training&quot;],
image: coach1,
},
{
id: 2,
name: &quot;BROOKLYN MOORE&quot;,
categories: [&quot;Pilates&quot;, &quot;Yoga&quot;],
image: coach2,
},
{
id: 3,
name: &quot;MILEY CYRUS&quot;,
categories: [&quot;HIIT&quot;, &quot;Strength Training&quot;, &quot;Strength Training&quot;],
image: coach1,
},
{
id: 4,
name: &quot;BROOKLYN MOORE&quot;,
categories: [&quot;Pilates&quot;, &quot;Yoga&quot;],
image: coach2,
},
];
const handleSwiperInit = (swiper: any) =&gt; {
setSwiper(swiper);
};
return (
&lt;div&gt;
&lt;Swiper
loop={true}
spaceBetween={10}
slidesPerView={2.2}
modules={[FreeMode, Navigation, Thumbs]}
className=&quot;mySwiper2&quot;
navigation={false}
onInit={handleSwiperInit}
&gt;
{coachData.map((item, index) =&gt; (
&lt;SwiperSlide key={index}&gt;
&lt;CoachCard coach={item} /&gt;
&lt;/SwiperSlide&gt;
))}
&lt;/Swiper&gt;
&lt;div className=&quot;mt-[50px] flex justify-center gap-4&quot;&gt;
&lt;span
className=&quot;cursor-pointer&quot;
onClick={() =&gt; {
swiper?.slidePrev();
}}
&gt;
&lt;Image src={arrowLeft} alt=&quot;arrowLeft&quot; /&gt;
&lt;/span&gt;
&lt;span
className=&quot;cursor-pointer&quot;
onClick={() =&gt; {
swiper?.slideNext();
}}
&gt;
&lt;Image src={arrowLeft} alt=&quot;arrowLeft&quot; className=&quot;rotate-180&quot; 
/&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
);
};
export default Slider;

答案2

得分: 0

我遇到了相同的问题,我正在使用 v10.3.0 版本,通过以下方式修复:

<Swiper
  spaceBetween={30}
  modules={[Navigation]}
  loop
  autoplay={{
    stopOnLastSlide: false,
  }}
>
  {/* 你的幻灯片内容 */}
</Swiper>
英文:

I faced the same problem I am using v10.3.0, fixed it by:

&lt;Swiper
spaceBetween={30}
modules={[Navigation]}
loop
autoplay={{
stopOnLastSlide: false,
}}
&gt; 
{/* Your Slides*/}
&lt;/Swiper&gt;

huangapple
  • 本文由 发表于 2023年2月27日 19:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75580024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定