gpt4 book ai didi

reactjs - 将父级 ​​onWheel 转发到滚动表

转载 作者:行者123 更新时间:2023-12-03 13:47:34 28 4
gpt4 key购买 nike

我有一个外部父代 <div />包含 <Table /> 的容器元素。我希望能够滚动 <Table />关于onWheel家长的事件<div />元素,即使鼠标当前未位于 <Table /> 上.

我有一个对表元素的引用和一个监听 onWheel 事件的事件处理程序,但我无法弄清楚如何将该事件转发到表。

最佳答案

因为我猜你想滚动表格主体,所以你可以尝试一下。

class Table extends React.Component {

constructor() {
super();
this.callback = this.callback.bind(this);
this.body = null;
}

callback(ev) {

this.body.scrollTop += ev.deltaY;
}

render() {
return <div onWheel={this.callback}>
<table>
<tbody ref={c => this.body = c}>
{[1, 2, 3, 4, 5, 6,].map(i => <tr>
<td>{i}</td>
</tr>)}
</tbody>
</table>
</div>;
}
}

ReactDOM.render(<Table />, document.getElementById('root'));
tbody {
display: block;
height: 3rem;
overflow: scroll;
}

tr:nth-child(2n+1) {
background-color: #ddf;
}

tr:nth-child(2n) {
background-color: #eef;
}

table {
margin: auto;
border-collapse: collapse;
}

td {
width: 5rem;
text-align: center;
font-family: sans-serif;
color: #00f;
}

div {
width: 100%;
display: block;
text-align: center;
background-color: #99f;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

关于reactjs - 将父级 ​​onWheel 转发到滚动表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47817297/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com