Wim*_*nsa 9 javascript scroll reactjs
我有简单的反应组件,我将onScroll事件设置为该组件,但是当我滚动它没有触发时
import React, { Component, PropTypes } from 'react'
export default class MyComponent extends Component {
_handleScroll(e) {
console.log('scrolling')
}
render() {
const style = {
width: '100px',
height: '100px',
overflowY: 'hidden'
}
const innerDiv = {
height: '300px',
width: '100px',
background: '#efefef'
}
return (
<div style={style} onScroll={this._handleScroll}>
<div style={innerDiv}/>
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
您需要向 DOM 元素添加引用:
class ScrollingApp extends React.Component {
_handleScroll(ev) {
console.log("Scrolling!");
}
componentDidMount() {
const list = ReactDOM.findDOMNode(this.refs.list)
list.addEventListener('scroll', this._handleScroll);
}
componentWillUnmount() {
const list = ReactDOM.findDOMNode(this.refs.list)
list.removeEventListener('scroll', this._handleScroll);
}
/* .... */
}
Run Code Online (Sandbox Code Playgroud)