React JS:onWheel 冒泡停止传播不起作用
React JS: onWheel bubbling stopPropagation not working
TL;DR:
查看我的 jsfiddle link,滚动 child div 直到 bottom/top 不应开始滚动 parent。我尝试使用 e.stopPropagation()
这没有用。所以需要解决方案(不使用 mixins)。
My Code:
//////////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')
);
My Target:
我有一个 div 可以滚动。但是在滚动 div 后到达顶部或底部时,滚动开始在整个页面本身上发生,这是我不想要的。我认为这是由于事件传播而发生的,所以我试图以以下方式停止。
My Implementation:
我正在使用 "extends Component (es6 way)" 创建我的组件。我已经将 onWheel 事件侦听器设置为
它正在安慰 'Scrolling Me' 很好,但我无法停止将此事件传播到 parent。
不确定为什么 stopPropagation 没有准确发生,或者这个问题是否因为传播而发生
我不想使用使用mixins的库,所以请不要这样建议我。
我没有足够的声誉来发表评论,但请在此处查看解决方案:
Scrolling child div scrolls the window, how do I stop that?
$.fn.scrollGuard2 = function() {
return this
.on( 'wheel', function ( e ) {
var $this = $(this);
if (e.originalEvent.deltaY < 0) {
/* scrolling up */
return ($this.scrollTop() > 0);
} else {
/* scrolling down */
return ($this.scrollTop() + $this.innerHeight() < $this[0].scrollHeight);
}
});
};
我通过以下方式解决了这个问题。
较早的解决方案:
const rdom = require('react-dom');
<div onMouseOver={() => {
const ele = rdom.findDOMNode(this);
if (ele.scrollTop === 0 && ele.scrollHeight === ele.clientHeight) {
return;
} else {
document.body.style.overflow = 'hidden';
}
}}
onMouseOut={() => {
document.body.style.overflow = 'auto';
}}
</div>
新解决方案:
(由于 Mac 和 Linux 操作滚动条的方式与 windows 不同,上面的解决方案很烦人,因为它删除并向文档添加滚动条,因此 reducing/increasing 它的宽度可以摇晃其他元素)
handleScroll(e) {
const ele = rdom.findDOMNode(this);
if (e.nativeEvent.deltaY <= 0) {
/* scrolling up */
if(ele.scrollTop <= 0) {e.preventDefault();}
}
else
{
/* scrolling down */
if(ele.scrollTop + ele.clientHeight >= ele.scrollHeight) {
e.preventDefault();
}
}
/*Look question for the placement of below CODE*/
onWheel={(e) => {this.handleScroll(e);}}
另一个解决方案:
您还可以找到如果 document/window/body 已滚动到您的可滚动 div。然后将这个可滚动的 div 设置为 position: fixed。修复了 属性 自动不让 document/window/body 获取 onWheel 事件的问题。
TL;DR:
查看我的 jsfiddle link,滚动 child div 直到 bottom/top 不应开始滚动 parent。我尝试使用 e.stopPropagation() 这没有用。所以需要解决方案(不使用 mixins)。
My Code:
//////////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')
);
My Target:
我有一个 div 可以滚动。但是在滚动 div 后到达顶部或底部时,滚动开始在整个页面本身上发生,这是我不想要的。我认为这是由于事件传播而发生的,所以我试图以以下方式停止。
My Implementation:
我正在使用 "extends Component (es6 way)" 创建我的组件。我已经将 onWheel 事件侦听器设置为
它正在安慰 'Scrolling Me' 很好,但我无法停止将此事件传播到 parent。 不确定为什么 stopPropagation 没有准确发生,或者这个问题是否因为传播而发生
我不想使用使用mixins的库,所以请不要这样建议我。
我没有足够的声誉来发表评论,但请在此处查看解决方案:
Scrolling child div scrolls the window, how do I stop that?
$.fn.scrollGuard2 = function() {
return this
.on( 'wheel', function ( e ) {
var $this = $(this);
if (e.originalEvent.deltaY < 0) {
/* scrolling up */
return ($this.scrollTop() > 0);
} else {
/* scrolling down */
return ($this.scrollTop() + $this.innerHeight() < $this[0].scrollHeight);
}
});
};
我通过以下方式解决了这个问题。
较早的解决方案:
const rdom = require('react-dom');
<div onMouseOver={() => {
const ele = rdom.findDOMNode(this);
if (ele.scrollTop === 0 && ele.scrollHeight === ele.clientHeight) {
return;
} else {
document.body.style.overflow = 'hidden';
}
}}
onMouseOut={() => {
document.body.style.overflow = 'auto';
}}
</div>
新解决方案: (由于 Mac 和 Linux 操作滚动条的方式与 windows 不同,上面的解决方案很烦人,因为它删除并向文档添加滚动条,因此 reducing/increasing 它的宽度可以摇晃其他元素)
handleScroll(e) {
const ele = rdom.findDOMNode(this);
if (e.nativeEvent.deltaY <= 0) {
/* scrolling up */
if(ele.scrollTop <= 0) {e.preventDefault();}
}
else
{
/* scrolling down */
if(ele.scrollTop + ele.clientHeight >= ele.scrollHeight) {
e.preventDefault();
}
}
/*Look question for the placement of below CODE*/
onWheel={(e) => {this.handleScroll(e);}}
另一个解决方案:
您还可以找到如果 document/window/body 已滚动到您的可滚动 div。然后将这个可滚动的 div 设置为 position: fixed。修复了 属性 自动不让 document/window/body 获取 onWheel 事件的问题。