【问题标题】:How Do I Stop Swiper.JS Thumbs From Automatically Transitioning?如何阻止 Swiper.JS 拇指自动转换?
【发布时间】:2021-06-06 17:54:38
【问题描述】:

我有一个用 react 制作的 swiper.js 滑块。如果我在滑动器中有 8 个图像,然后我导航到第 8 个缩略图并单击第 7 个缩略图,它将向上滑动缩略图部分。有没有办法防止这种行为发生?

我想要做的是只有在有更多向上或向下的幻灯片可用时才自动滑动。例如,如果我有 5 个缩略图并且幻灯片 [2, 3, 4, 5, 6] 可见,如果我选择幻灯片 6,它将移动并显示幻灯片 7,如果我选择幻灯片 2,它将滑过并显示幻灯片 1。这是我希望它做的唯一动作。文档中是否提供该功能?

          <div className="mainSliderWrap">
            <Swiper
              pagination
              onSlideChange={swiper => {
                setImageSelectedIndex(swiper.activeIndex + 1)
                return true
              }}
              thumbs={{ swiper: thumbsSwiper }}>
              {products.images.map((product, index) => (
                <SwiperSlide key={product.originalSrc}>
                  <img
                    className={classes.mainSliderImg}
                    ref={el => {
                      imagesRef.current[index] = el as HTMLImageElement
                    }}
                    src={product.originalSrc}
                    data-zoom={product.originalSrc}
                    alt={product.title}
                    height={360}
                    width={360}
                  />
                </SwiperSlide>
              ))}
            </Swiper>
          </div>
          <div
            className={[
              "productSlider",
              ProductSliderOrientation(width) === "horizontal" &&
                "horizontalProductSlider"
            ].join(" ")}>
            <div className="swiper-button-next mainProdNext" />
            <div className="swiper-button-prev mainProdPrev" />
            <Swiper
              direction={ProductSliderOrientation(width)}
              touchRatio={1}
              threshold={10}
              slidesPerView={slidesPerView}
              spaceBetween={15}
              navigation={{
                nextEl: ".mainProdNext",
                prevEl: ".mainProdPrev"
              }}
              onSwiper={setThumbsSwiper}
              breakpoints={{
                0: {
                  spaceBetween: 10,
                  slidesOffsetBefore: 20,
                  slidesOffsetAfter: 20
                },
                576: {
                  spaceBetween: 10,
                  slidesPerView: 5
                },
                768: {
                  touchRatio: 0,
                  slidesPerView: 5
                },
                992: {
                  touchRatio: 1,
                  slidesPerView: 5
                },
                1200: {
                  touchRatio: 0,
                  slidesPerView: 5
                }
              }}>
              {products &&
                products.images.map(product => (
                  <SwiperSlide key={product.originalSrc}>
                    <ProductImage
                      image={product}
                      alt={products.title}
                      moduleClass={classes.productImagePick}
                      gatsbyImageClass={classes.productImagePickGatsby}
                    />
                  </SwiperSlide>
                ))}
            </Swiper>
          </div>
        </div>
      </div>

【问题讨论】:

  • 拇指的数量实际上与您的问题没有任何关系,因此请删除您描述的那部分以避免混淆

标签: javascript reactjs swiper swiper.js


【解决方案1】:

看起来没有办法在不处理数据的情况下实现具有这种行为的缩略图。所以想法是,当您更改缩略图时,您需要重新排序数据以显示下一个/上一个缩略图。

export default function App() {
  const [thumbsSwiper, setThumbsSwiper] = useState(null);

  const [data, setData] = useState([
    "https://swiperjs.com/demos/images/nature-1.jpg",
    "https://swiperjs.com/demos/images/nature-2.jpg",
    "https://swiperjs.com/demos/images/nature-3.jpg",
    "https://swiperjs.com/demos/images/nature-4.jpg",
    "https://swiperjs.com/demos/images/nature-5.jpg",
    "https://swiperjs.com/demos/images/nature-6.jpg",
    "https://swiperjs.com/demos/images/nature-7.jpg",
    "https://swiperjs.com/demos/images/nature-8.jpg",
    "https://swiperjs.com/demos/images/nature-9.jpg",
    "https://swiperjs.com/demos/images/nature-10.jpg"
  ]);

  return (
    <>
      <Swiper
        style={{
          "--swiper-navigation-color": "#fff",
          "--swiper-pagination-color": "#fff"
        }}
        loop={false}
        spaceBetween={10}
        navigation={true}
        thumbs={{ swiper: thumbsSwiper }}
        className="mySwiper2"
        onSlideChange={(e) => {
          if (e.realIndex % 3 === 0) {
            // move first element to the end
            const newData = [...data];
            newData.push(newData.shift());
            setData(newData);
            e.slideTo(e.realIndex - 1);
          }
          if (e.realIndex === 0) {
            // move last element to the beginning
            const newData = [...data];
            newData.unshift(newData.pop());
            setData(newData);
            e.slideTo(e.realIndex + 1);
          }
        }}
      >
        {data.map((img) => (
          <SwiperSlide key={img}>
            <img src={img} />
          </SwiperSlide>
        ))}
      </Swiper>
      <Swiper
        onSwiper={setThumbsSwiper}
        loop={false}
        loopedSlides={1}
        spaceBetween={10}
        slidesPerView={4}
        freeMode={false}
        watchSlidesVisibility={true}
        watchSlidesProgress={true}
        className="mySwiper"
      >
        {data.map((img) => (
          <SwiperSlide key={img}>
            <img src={img} />
          </SwiperSlide>
        ))}
      </Swiper>
    </>
  );
}

这是codesandbox

这只是一个简单的例子。希望对你有帮助。

【讨论】:

  • 添加分页设置没有任何影响
  • @Jason Biondo,我误解了你的要求,请查看更新帖