【发布时间】:2016-01-14 13:32:12
【问题描述】:
TL;DR:
查看我的 jsfiddle 链接,其中将子 div 向上滚动到底部/顶部不应开始滚动父级。我尝试使用 e.stopPropagation() 这没有用。所以需要解决方案(不使用 mixins)。
我的代码:
//////////HTML CODE
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
////////CSS CODE
.childScroll{
max-height: 200px;
overflow-y: scroll;
background-color: white;
max-width: 200px;
margin: 0 auto;
border: 1px solid blue
}
.parentScroll {
text-align: center;
max-height: 300px;
overflow-y: scroll;
background-color: lightgreen;
padding: 10px;
}
////////JS CODE
class Hello extends React.Component {
render() {
return (
<div className='parentScroll'>
Scrollable Parent: <br /><br />
<div className='childScroll' onWheel = {(e)=>{console.log('Scrolling Me..'); e.stopPropagation();}}>
Scroll CHILD LIST:1 <br /><br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
Some text blah<br />
</div>
<div className='childScroll' onWheel = {(e)=>{console.log('Parent got scroll event');}}>
Scroll CHILD LIST:2 <br /><br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
Hi text blah<br />
</div>
</div>
);
}
}
ReactDOM.render(
<Hello name="World" />,
document.getElementById('container')
);
我的目标:
我有一个滚动的 div。但是在滚动 div 后到达顶部或底部时,滚动开始发生在我不想要的整个页面本身上。我认为这是由于事件传播而发生的,所以我试图以下列方式停止。
我的实现:
我正在使用“扩展组件(es6 方式)”来创建我的组件。我把 onWheel 事件监听器设置为
“滚动我”很好,但我无法停止将此事件传播给父级。 不知道为什么 stopPropagation 没有完全发生,或者这个问题是否因为传播而发生
我不想使用使用 mixins 的库,所以请不要这样建议我。
【问题讨论】: