简体   繁体   中英

Swiper Js vertical loop doesn't work properly for react

I tried to design a time picker component for reactjs like iOS time picker.

I used two vertical swiper one for hour and another for minute.

在此处输入图像描述

as you see in the above photo when i swipe bottom it's not working properly. it shows 59,58, 57 and suddenly nothing an you shoud swiper again to show 56, 55, ... . it has same issue in hour picker. it show 23, 22, 21 and nothing and need to swiper again. here is my code:

CodeSandbox Link

import { Swiper, SwiperSlide } from 'swiper/react';
import styles from "./timePicker.module.scss"

const hours = Array.from(Array(24).keys())
const minutes = Array.from(Array(60).keys())

const TimePicker = () => {
    return (
        <div className={styles.timePicker}>
            <div className={styles.picker}>
                <Swiper
                    slidesPerView={3}
                    direction="vertical"
                    grabCursor
                    centeredSlides
                    loop
                >
                    {minutes.map(minute=>{
                        return (
                            <SwiperSlide key={minute}>
                                <div className={styles.time}>
                                    {(''+minute).padStart(2,'0')}
                                </div>
                            </SwiperSlide>
                        )
                    })}
                </Swiper>
            </div>
            <div>:</div>
            <div className={styles.picker}>
                <Swiper
                    slidesPerView={3}
                    direction="vertical"
                    centeredSlides
                    grabCursor
                    loop
                    autoplay
                >
                    {hours.map(hour=>{
                        return (
                            <SwiperSlide key={hour}>
                                <div className={styles.time}>
                                    {(''+hour).padStart(2,'0')}

                                </div>
                            </SwiperSlide>
                        )
                    })}
                </Swiper>
            </div>
        </div>
    );
}

export default TimePicker;

This is a bug? or i do something wrong?

Thanks

Because with such a "small" slider you apparently can swipe a lot more than its size, and Swiper doesn't do anything during swipe. Just increase loopedSlides parameter until you get desired behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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