Mouser wheel actions can be detected using onWheel property. Here we will use deltaY property of onWheel event to find direction mouse wheel.

To Run Example Code

execute below commands
npm install
npm start
now open index.html in browser

Program

Observe below program. Whenever user scroll mouse wheel on image, detect scroll direction and increase or decrease width of image based on that

import React from 'react';
import classNames from 'classnames';

class ZoomImage extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      width:this.props.width
    };
    this.hadleMouseWheel = this.hadleMouseWheel.bind(this);
  }

  hadleMouseWheel(evt) {
    if(evt.deltaY > 0) {
        this.setState({
        width:(this.state.width - 5)
      });
    } else if(evt.deltaY < 0) {
      this.setState({
        width:(this.state.width + 5)
      });
    }
  }

  render() {
    const imgStyle = {
      width:this.state.width+'px'
    };
    const {width, ...props} = this.props;
    return (
      <img {...props} style={imgStyle} onWheel={this.hadleMouseWheel}/>
    );
  }
}

ZoomImage.propTypes = {
  width:React.PropTypes.number
}

ZoomImage.defaultProps = {
  width:400
}

export default ZoomImage;

1 comment:

Blogroll

Popular Posts