akiran / react-slick

React carousel component
http://react-slick.neostack.com/
MIT License
11.61k stars 2.09k forks source link

Sliding on mobile device scrolls entire page #1281

Open randallreedjr opened 6 years ago

randallreedjr commented 6 years ago

I have only seen this happen on an actual mobile device, not in the mobile views available in either Chrome or Safari developer tools.

It appears to only affect iOS (confirmed on iPhone running 11.3 and iPhone 8 Plus running 11.4) and not Android.

React Slick mobile

react-slick-mobile

Slick mobile

slick-mobile

randallreedjr commented 6 years ago

May share a root cause with #1240

randallreedjr commented 6 years ago

This was my solution:

  constructor(props) {
    super(props);

    this.carouselSettings = {..., touchThreshold: 5, ...}; // explicitly set touchThreshold since we reference it later
    this.preventTouch = this.preventTouch.bind(this);
    this.touchStart = this.touchStart.bind(this);
  }

  componentDidMount() {
    // Disable touchmove to prevent scrolling entire page
    const carousel = document.getElementById('someid'); // Your site element containing react-slick's carousel-container
    carousel.addEventListener('touchstart', this.touchStart);
    carousel.addEventListener('touchmove', this.preventTouch, { passive: false });
  }

  touchStart(e) {
    // capture user's starting finger position, for later comparison
    this.firstClientX = e.touches[0].clientX;
  }

  preventTouch(e) {
    // only prevent touch on horizontal scroll (for horizontal carousel)
    // this allows the users to scroll vertically past the carousel when touching the carousel
    // this also stabilizes the horizontal scroll somewhat, decreasing vertical scroll while horizontal scrolling
    const clientX = e.touches[0].clientX - this.firstClientX;
    const horizontalScroll = Math.abs(clientX) > this.carouselSettings.touchThreshold;
    if (horizontalScroll) {
      e.preventDefault();
    }
  }

Thanks to @yunyong for his helpful comment.

renetalk commented 5 years ago

You can also solve this by giving the surrounding container a overflow: hidden

Eth3rnit3 commented 5 years ago

That's right, @renetalk , but I'd like to point out that it's the react-slick container.

.slick-slider {
  overflow: hidden;
}
kingalione commented 5 years ago

Problem with the overflow: hidden hack is that the dots are then missing and I'm not able to bring them back somehow

kingalione commented 5 years ago

Or just disable the arrows thats working better.

AndersonFerreira07 commented 4 years ago

.slick-slider { overflow: hidden; }

funcional para mim, valeu mesmo!!!

p-akash commented 4 years ago

.slick-slider { overflow: hidden; } now dots are gone

ghost commented 4 years ago

.slick-slider { overflow: hidden; } .slick-dots { position: static; }

Now It will works.

lordival commented 1 year ago

.slick-slider { overflow: hidden; } .slick-dots { position: static; }

Now It will works.

still not working for me. dots still gone

mgurjanov commented 1 year ago

I used this solution:

.slick-list { max-width: 99%; }

Overflow X is gone and dots are visible. You might need to set/unset margins on mobile to center active slide.

Miguel-Angel-Guate commented 3 months ago

I used this solution because with the overflow hidden the dots was gone and with the line below it´s now dots are back .slick-slider { overflow: hidden; padding-bottom: 40px; } .slick-dots { //here you can customize the height height: 10vh; }