question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disable window scrolling while swiping horizontally [MOBILE]

See original GitHub issue

When you start scrolling on mobile(Safari && Chrome from iOS) it is able to scroll vertically and thats bad UX. Is there any workarounds for that case. My settings: let settings = { className: this.props.className || '', infinite: true, lazyLoad: true, speed: 200, arrows: this.props.arrows, swipeToSlide: true, touchMove: true, nextArrow: <NextArrow/>, prevArrow: <PrevArrow/>, afterChange: this.props.afterChange, beforeChange: this.props.beforeChange, };

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:18
  • Comments:22

github_iconTop GitHub Comments

119reactions
yunyongcommented, Jun 12, 2018

How about this?

componentDidMount(){
    window.addEventListener('touchstart', this.touchStart);
    window.addEventListener('touchmove', this.preventTouch, {passive: false});
}

componentWillUnmount(){
    window.removeEventListener('touchstart', this.touchStart);
    window.removeEventListener('touchmove', this.preventTouch, {passive: false});
}

touchStart(e){
    this.firstClientX = e.touches[0].clientX;
    this.firstClientY = e.touches[0].clientY;
}

preventTouch(e){
    const minValue = 5; // threshold

    this.clientX = e.touches[0].clientX - this.firstClientX;
    this.clientY = e.touches[0].clientY - this.firstClientY;

    // Vertical scrolling does not work when you start swiping horizontally.
    if(Math.abs(this.clientX) > minValue){ 
        e.preventDefault();
        e.returnValue = false;
        return false;
    }
}
29reactions
xavi-tristanchocommented, Jul 19, 2019

following the code by @yunyong I’ve come up with a solution that doesn’t break the window horizontal scroll by adding the addEventListener to a div containing the react-slick component instead of the window object.

Also @iDVB I don’t think it’s a good idea to save the firstClientX, firstClientY, clientX into the state as this values do not affect the component render at all, furthermore, the setX calls are slower than just assigning the new values to normal variables defined outside the component given that the touchstart and touchmove are called about 10 times by one simple little scroll.

let firstClientX, clientX;

const preventTouch = e => {
  const minValue = 5; // threshold

  clientX = e.touches[0].clientX - firstClientX;

  // Vertical scrolling does not work when you start swiping horizontally.
  if (Math.abs(clientX) > minValue) {
    e.preventDefault();
    e.returnValue = false;

    return false;
  }
};

const touchStart = e => {
  firstClientX = e.touches[0].clientX;
};

const Slider = ({ children, ...props }) => {
  let containerRef = createRef();

  useEffect(() => {
    if (containerRef.current) {
      containerRef.current.addEventListener("touchstart", touchStart);
      containerRef.current.addEventListener("touchmove", preventTouch, {
        passive: false
      });
    }

    return () => {
      if (containerRef.current) {
        containerRef.current.removeEventListener("touchstart", touchStart);
        containerRef.current.removeEventListener("touchmove", preventTouch, {
          passive: false
        });
      }
    };
  });

  return (
    <div ref={containerRef}>
      <ReactSlick {...settings} {...props}>
        {children}
      </ReactSlick>
    </div>
  );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Disable vertical scrolling while swiping on touch device using ...
I want to disable vertical scrolling on webpage while swiping the carousel horizontally on mobile devices. I'm using the Owl carousel.
Read more >
Prototype scrolling with overflow behavior - Figma Help Center
Horizontal scrolling allows users to swipe or scroll left and right within a frame, while maintaining their vertical position. Use this to create...
Read more >
How To Create Horizontal Scrolling Containers - codeburst
On our container, we want to turn off vertical scrolling (overflow-y) and enable horizontal scrolling (overflow-x). Then with each card, we want to...
Read more >
How To Fix Horizontal Scrollbar on Mobile When Using ...
The horizontal scrollbar issue on mobile devices can be fixed by setting overflow to hidden. You can do this by using the Elementor...
Read more >
How To Prevent Scrolling The Page On iOS Safari 15 - PQINA
We set the height of both the html and the body element to the window height and then set overflow on these elements...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found