我有一个简单的react组件,我为该组件设置了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>
)
}
}发布于 2017-02-18 21:06:04
您需要将overflowY的值更改为auto或scroll。现在你得不到滚动条,因为hidden会导致浏览器隐藏滚动条。
发布于 2017-02-17 23:22:20
您需要向DOM元素添加一个ref:
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);
}
/* .... */
}发布于 2017-02-18 20:43:01
您需要在构造函数中绑定_handleScroll事件。尝试将此代码添加到组件中。
constructor() {
this._handleScroll = this._handleScroll.bind(this);
}https://stackoverflow.com/questions/42300837
复制相似问题