Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in loop mode, the component events can't be worked if swipe from the first slide to the last slide #85

Closed
xinconan opened this issue Oct 27, 2017 · 7 comments

Comments

@xinconan
Copy link

in loop mode, the component events can't be worked if swipe from the first slide to the last slide.
for example:

<Swiper {...params}>
  {
      imgList.map((list, i) =>
        <img key={i} onClick={this.onDocClick.bind(this, list.url)} src={list.imgUrl} />
      )
   }
 </Swiper>

the onClick method will worked as well in no-loop, but in loop mode, if swipe from the first slide to the last slide, it couldn't worked.
I know the Swiper will clone the first and the last of the element in loop mode, how to make it right in react?

@gareys
Copy link
Contributor

gareys commented Nov 6, 2017

try wrapping your swiper slides with a div. Let's see if that helps.

<div key={i}>
  <img ... />
</div>

@OmgDef
Copy link

OmgDef commented Dec 4, 2017

Faced the same issue. Wrapping into with div doesn't help.

@gareys
Copy link
Contributor

gareys commented Dec 7, 2017

After thinking more about this. I attempted to use loop mode and noticed that it doesn't work very well in my implementation. Soon, I will be working on making loop mode work in a project of mine and will update this with details.

@razor1895
Copy link

Because of nature of how the loop mode works, swiper will add duplicated slides, you will see a copy of last slide when you swipe from the first slide to the last. This copy is not under the manage of react, it will not respond to react events. So you need to add a click eventListener to handle this

componentDidMount() {
    const slides = document.querySelectorAll('.swiper-slide');
    const fakeFirstSlide = slides[slides.length - 1];
    const fakeLastSlide = slides[0];

    if (this.props.onClickFirstSlide) {
      fakeFirstSlide.addEventListener('click', this.props.onClickFirstSlide);
    }

    if (this.props.onClickLastSlide) {
      fakeLastSlide.addEventListener('click', this.props.onClickLastSlide);
    }
  }

@kidjp85 kidjp85 closed this as completed Jan 24, 2018
@crohrer
Copy link

crohrer commented Sep 19, 2018

This is more a workaround than a real solution to the problem. This gets very messy when you need to track multiple events. Do you plan to implement a real fix, or are react-events in loop-mode not supported?

@zxllzw
Copy link

zxllzw commented Sep 9, 2020

onClickFirstSlide

where is onClickFirstSlide & onClickLastSlide?

@zeejay09
Copy link

Because of nature of how the loop mode works, swiper will add duplicated slides, you will see a copy of last slide when you swipe from the first slide to the last. This copy is not under the manage of react, it will not respond to react events. So you need to add a click eventListener to handle this

componentDidMount() {
    const slides = document.querySelectorAll('.swiper-slide');
    const fakeFirstSlide = slides[slides.length - 1];
    const fakeLastSlide = slides[0];

    if (this.props.onClickFirstSlide) {
      fakeFirstSlide.addEventListener('click', this.props.onClickFirstSlide);
    }

    if (this.props.onClickLastSlide) {
      fakeLastSlide.addEventListener('click', this.props.onClickLastSlide);
    }
  }

How to implement this if slidesPerView is set to auto and instead of onClick, the listener used is onMouseEnter and onMouseOut?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants