您可以将 Grid 包装在 react-custom-scrollbars 中并传递 onScroll 事件。这段代码对我有用。
import React from 'react';
import { Grid } from 'react-virtualized';
import { Scrollbars } from 'react-custom-scrollbars';
class ScrollableGrid extends React.Component {
handleScroll = ({ target }) => {
const { scrollTop, scrollLeft } = target;
this.Grid.handleScrollEvent({ scrollTop, scrollLeft });
};
Grid = null;
render() {
const { width, height } = this.props;
return (
<Scrollbars autoHide style={{ width, height }} onScroll={this.handleScroll}>
<Grid
{...this.props}
ref={instance => (this.Grid = instance)}
style={{
overflowX: false,
overflowY: false
}}
/>
</Scrollbars>
);
}
}
const list = [
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125],
['Brian Vaughn', 'Software Engineer', 'San Jose', 'CA', 95125]
];
ReactDOM.render(
<ScrollableGrid
cellRenderer={({ columnIndex, key, rowIndex, style }) => {
return (
<div key={key} style={style}>
{list[rowIndex][columnIndex]}
</div>
);
}}
columnCount={list[0].length}
columnWidth={100}
height={300}
rowCount={list.length}
rowHeight={30}
width={300}
/>,
document.getElementById('example')
);
您可以在 github issue中查看 List 示例和更多上下文