mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-01 11:39:28 +08:00
Only listen mousemove once
初始化时监听, 共享同一个 mousePosition
This commit is contained in:
parent
f4964626d9
commit
ee8335856c
@ -43,4 +43,3 @@ var Test = React.createClass({
|
||||
|
||||
React.render(<Test/> , document.getElementById('components-modal-demo-basic'));
|
||||
````
|
||||
|
||||
|
14
components/modal/eventListener.js
Normal file
14
components/modal/eventListener.js
Normal file
@ -0,0 +1,14 @@
|
||||
function wrap(standard, fallback) {
|
||||
return function (el, evtName, listener, useCapture) {
|
||||
if (el[standard]) {
|
||||
el[standard](evtName, listener, useCapture);
|
||||
} else if (el[fallback]) {
|
||||
el[fallback]('on' + evtName, listener);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
add: wrap('addEventListener', 'attachEvent'),
|
||||
remove: wrap('removeEventListener', 'detachEvent')
|
||||
};
|
@ -1,22 +1,18 @@
|
||||
import React from 'react';
|
||||
import Dialog from 'rc-dialog';
|
||||
import eventListener from './eventListener';
|
||||
function noop() {
|
||||
}
|
||||
|
||||
function wrap(standard, fallback) {
|
||||
return function (el, evtName, listener, useCapture) {
|
||||
if (el[standard]) {
|
||||
el[standard](evtName, listener, useCapture);
|
||||
} else if (el[fallback]) {
|
||||
el[fallback]('on' + evtName, listener);
|
||||
}
|
||||
let mousePosition;
|
||||
function onDocumentMousemove(e) {
|
||||
mousePosition = {
|
||||
x: e.pageX,
|
||||
y: e.pageY
|
||||
};
|
||||
}
|
||||
|
||||
let eventListener = {
|
||||
add: wrap('addEventListener', 'attachEvent'),
|
||||
remove: wrap('removeEventListener', 'detachEvent')
|
||||
};
|
||||
eventListener.add(document, 'mousemove', onDocumentMousemove);
|
||||
|
||||
export default React.createClass({
|
||||
getDefaultProps() {
|
||||
@ -41,14 +37,6 @@ export default React.createClass({
|
||||
};
|
||||
},
|
||||
|
||||
componentWillMount() {
|
||||
eventListener.add(document, 'mousemove', this.onDocumentMousemove);
|
||||
},
|
||||
|
||||
componentWillUnmount() {
|
||||
eventListener.remove(document, 'mousemove', this.onDocumentMousemove);
|
||||
},
|
||||
|
||||
handleCancel() {
|
||||
this.props.onCancel();
|
||||
this.setState({
|
||||
@ -92,6 +80,6 @@ export default React.createClass({
|
||||
let visible = this.state.visible;
|
||||
return <Dialog transitionName="zoom" onClose={this.handleCancel}
|
||||
maskAnimation="fade" width="500" footer={footer} {...props}
|
||||
visible={visible} mousePosition={this.mousePosition} />;
|
||||
visible={visible} mousePosition={mousePosition} />;
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user