Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
How to make Swiper carousel change slides by clicking space in React?

I use "swiper": "6.8.4" in React app for carousel.

I need to change slides by clicking on the "Space" button. By default it changes by clicking on arrows. Could you please advise me how?

function Carousel() {

  document.addEventListener("keydown", function(e){
    if(KeyboardEvent.code == 'Space') {
      swiper.slideNext(); //not working
    }
    if(e.keyCode == 32) {
     swiper.slideNext(); //not working
    }
  });

  return (
    <div className={classes.carouselWrapper}>
      <Swiper
        autoplay={{
          delay: 2500,
          disableOnInteraction: false,
        }}
        keyboard={{
          enabled: true,
          onlyInViewport: true,
        }}
        pagination={true}
        className="mySwiper"
        cssMode={true}
        loop={false}
        scrollbar={{draggable: true}}
        modules={[Autoplay, Navigation, Pagination, Keyboard]}
        mousewheel={true}
        onSwiper={swiper => console.log(swiper)}
        slidesPerView={1}
      >
        <button>
          Change slide by clicking space
        </button>
        <SwiperSlide>
          1
        </SwiperSlide>
        <SwiperSlide>
          2
        </SwiperSlide>
     </Swiper>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

document.body.onkeyup = function(e){ if(e.keyCode == 32){ swiper.slideNext(); }

Can you also check if any additional library is using an event listener for spacebarr which might interrupt you're process

about 4 years ago · Juan Pablo Isaza Report

0

First thing you want to do is save swiper to the state so you can call slideNext, you can do this by using the onSwiper function. Then use useEffect to add and remove the keydown listener.

function Carousel() {
  const [swiper, setSwiper) = useState(null);

  useEffect(() => {
    const keydownHandler = e => {
      if(swiper && e.keyCode == 32) {
        e.preventDefault();
        swiper.slideNext();
      }
    }

    document.addEventListener('keydown', keydownHandler);

    return () => {
      document.removeEventListener('keydown', keydownHandler);
    }
  }, [swiper])

  return (
    <div className={classes.carouselWrapper}>
      <Swiper
        autoplay={{
          delay: 2500,
          disableOnInteraction: false,
        }}
        keyboard={{
          enabled: true,
          onlyInViewport: true,
        }}
        pagination={true}
        className="mySwiper"
        cssMode={true}
        loop={false}
        scrollbar={{draggable: true}}
        modules={[Autoplay, Navigation, Pagination, Keyboard]}
        mousewheel={true}
        onSwiper={setSwiper}
        slidesPerView={1}
      >
        <button>
          Change slide by clicking space
        </button>
        <SwiperSlide>
          1
        </SwiperSlide>
        <SwiperSlide>
          2
        </SwiperSlide>
     </Swiper>
    )
}

Keep in mind that space is used by browsers to scroll the page down, you probably don't want to prevent the default action.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!