akiran / react-slick

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

Horizotal Swipe / Scroll mouse event #2085

Open umerjaved178 opened 3 years ago

umerjaved178 commented 3 years ago

I have implemented my slider with react-slick, everything is working fine (https://react-slick.neostack.com/docs/example/multiple-items)

Now I want to add extra functionality that Swiping fingers across the laptop mouse pad will cause Slider Items to move/slide (horizontal scrolling)

You can see the desired functionality at https://www.apple.com/store

I am not able to find this option in documentation, not even on Stackoverflow, can anyone help me with that?

sxrthxk commented 3 years ago

Create a onWheel event on the slider, then hook it to a function that checks the deltaX value of the event. Use this with the slickNext() method provided by react-slick to change slides on horizontal scroll.

EasyBreezy97 commented 2 years ago
  const onWheelSlider = debounce((e, ref) => {
    if (!ref.current) return;

    if (e.deltaX > 0) {
      ref.current.slickNext();
    } else if (e.deltaX < 0) {
      ref.current.slickPrev();
    }
  }, 20);
//jsx
   <div onWheel={onWheelSlider}>
     <Slider ref={sliderRef}>
         ...
     </Slider
   </div>