Mouse wheel events in Reactjs

后端 未结 2 1656
感动是毒
感动是毒 2021-02-19 02:17

How do you go about getting mouse wheel events in reactjs?

I have tried onWheel

  render: function() {
    return 
相关标签:
2条回答
  • 2021-02-19 02:51

    First, your div is 0 height, so you don't scroll on it. You don't have to bind this as it is a class (and not an es6 react component). Just do a call to the function with the event as parameter on onWheel event:

    https://jsfiddle.net/812jnppf/9/

    render: function() {
    
       return <div style={{height:300, width:300}} onWheel = {(e) => this.wheel(e)} >      < /div>;
    },
    

    Of course, you have to clean code to style your div with a var or in css.

    EDIT : I set it onWheel and not on onScroll (witch doesn't exist I guess ? confirm it if you know). I saw a SO post about adding scroll event to your component easilly : https://stackoverflow.com/a/29726000/4099279

    0 讨论(0)
  • 2021-02-19 03:01

    Bind the callback to this:

    render: function() {
        return <div onWheel = {this.wheel.bind(this)} >
           < /div>;
    }
    
    0 讨论(0)
提交回复
热议问题