4

这是我正在使用的轮播:react-slick

我希望能够使用鼠标向上或向下滚动事件滚动浏览每张幻灯片。

向上滚动以增加,向下滚动以减少。

在网上找到了我所需要的示例-只是不确定如何将其转换为反应解决方案。

示例:https ://codepen.io/Grawl/pen/mMLQQb

在基于“反应”组件的方法中实现这一目标的最佳方法是什么?

这是我的反应组件:

import React from 'react';
import PropTypes from 'prop-types';
import styles from './styles.css';
import ReactSVG from 'react-svg';
import Slider from 'react-slick';


import MobileSVG from '../../../assets/svg/icons/Mobile_Icon_Option2.svg';
import TabletSVG from '../../../assets/svg/icons/Tablet_Icon_Option2.svg';
import DesktopSVG from '../../../assets/svg/icons/Desktop_Icon_Option2.svg';

const deviceIcons = {'mobile': MobileSVG, 'tablet': TabletSVG, 'desktop': DesktopSVG};

import BackToTopButton from '../BackToTopButton';

export default class ProductComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        };

    }

    render() {
        const {productData} = this.props

        //Slider settings
        const settings = {
            dots: true,
            infinite: false,
            speed: 500,
            fade: true,
            arrows: false,
            centerMode: true,
            slidesToShow: 1,
            slidesToScroll: 1
        }

        //Slider items
        const sliderItems = productData.map((obj, i) => {
            return (
                <div className="product-component row" key={i}>
                    <div className="product-component-image-wrap col-xs-12 col-sm-8">
                        <span className="product-heading">{obj.category}</span>
                        <div className="product-detail-wrap">
                            <img className="product-component-image" src={`${process.env.DB_URL}${obj.image}`} />
                            <ul className="list-device-support">
                                {obj.categoryDeviceSupport.map((obj, i) => {
                                    return (<li key={i}>
                                        <span className="svg-icon">
                                            <ReactSVG path={deviceIcons[obj.value]} />
                                        </span>
                                        <span className="product-label">{obj.label}</span>
                                    </li>)
                                })}
                            </ul>
                        </div>
                    </div>
                    <div className="product-component-info col-xs-12 col-sm-3"> 
                        <span className="align-bottom">{obj.title}</span>
                        <p className="align-bottom">{obj.categoryBrief}</p>
                    </div>
                </div>
            )
        });
        return (
            <div className="product-component-wrap col-xs-12">
                <Slider {...settings}>
                    {sliderItems}
                </Slider>
                <BackToTopButton scrollStepInPx="50" delayInMs="7" />
            </div>
        )
    }
}

ProductComponent.propTypes = {
    productData: PropTypes.array
};

ProductComponent.defaultProps = {
    productData: []
};
4

5 回答 5

8

你想做这样的事情:

constructor(props){
    super(props);
    this.slide = this.slide.bind(this);
}
slide(y){
    y > 0 ? (
       this.slider.slickNext()
    ) : (
       this.slider.slickPrev()
    )
}
componentWillMount(){
    window.addEventListener('wheel', (e) => {
        this.slide(e.wheelDelta);
    })
}
render(){...

并为您的滑块添加一个参考:

<Slider ref={slider => this.slider = slider }>

因此,当滚轮事件的 y 值大于 0 时,即向上滚动显示下一张幻灯片,向下滚动显示上一张。

于 2017-12-04T21:27:09.067 回答
6

The following should work fine for you:

componentDidMount(){
  let slickListDiv = document.getElementsByClassName('slick-list')[0]
  slickListDiv.addEventListener('wheel', event => {
    event.preventDefault()
    event.deltaY > 0 ? this.slider.slickNext() : this.slider.slickPrev()
  })
}

You should initialize the component like this:

<Slider {...settings} ref={slider => this.slider = slider.innerSlider}>
...
</Slider>
于 2018-01-11T06:47:05.073 回答
1

我在我的 CustomSlider 组件中使用以下代码:

  constructor(props) {
    super(props);
    this.handleWheel = this.handleWheel.bind(this);
  }

  componentDidMount() {
    ReactDOM.findDOMNode(this).addEventListener('wheel', this.handleWheel);
  }

  componentWillUnmount() {
    ReactDOM.findDOMNode(this).removeEventListener('wheel', this.handleWheel);
  }

  handleWheel(e) {
    e.preventDefault();
    e.deltaY > 0 ? this.slider.slickNext() : this.slider.slickPrev();
  }

组件初始化

 <Slider ref={slider => this.slider = slider}>
   ...
 </Slider>
于 2019-04-02T13:30:26.533 回答
0

带挂钩

const sliderRef = createRef();
const scroll = useCallback(
    y => {
      if (y > 0) {
        return sliderRef?.current?.slickNext(); /// ? <- using description below 
      } else {
        return sliderRef?.current?.slickPrev();
      }
    },
    [sliderRef]
  );
 useEffect(() => {
    window.addEventListener("wheel", e => {
      scroll(e.deltaY);
    });
  }, [scroll]);

我使用了与 babel 插件连接的 typescript 的可选链接,但您可以使用如下验证:

sliderRef.current && sliderRef.current.slickNext()

于 2020-10-04T08:46:52.243 回答
0

我能够使用钩子(useRef 获取对 Slider 组件的引用和 useEffect 向车轮事件)。

const myComponent () => {

 const settings = {
    dots: true,
    slidesToShow: 1,
    slidesToScroll: 1,};

const slider = useRef(null);

function scroll(e){
    if (slider === null)
        return 0;

    e.wheelDelta > 0 ? (
        slider.current.slickNext()
    ) : (
        slider.current.slickPrev()
    );

};

useEffect(() => {
    window.addEventListener("wheel", scroll,true);

    return () => {
        window.removeEventListener("wheel", scroll, true);
    };
}, []);

return (
 <Slider {...settings} ref={slider}>
 </Slider>
);
}

export default myComponent;
于 2021-08-16T15:27:27.883 回答