我有一个svg矩形,其4个边上有4个resizer,矩形和resizer都有onmousedown / onmousemove / onmouseup事件监听器。
当我从调整器调整矩形大小时,当我停止调整元素大小或者可能没有触发onmouseup时,大小调整器的onmousemove不会停止。
这是我的代码:
用于拖放的矩形事件:
onMouseDown = (e) => { if ( this.state.isDraggable ) { document.addEventListener('mousemove', this.onMouseMove); this.coords = { x: e.clientX, y: e.clientY } } } onMouseUp = (e) => { // this.props.updateStateDragging(this.props.id, false); document.removeEventListener('mousemove', this.onMouseMove); this.coords = {}; } onMouseMove = (e) => { const xDiff = this.coords.x - e.clientX; const yDiff = this.coords.y - e.clientY; this.coords.x = e.clientX; this.coords.y = e.clientY; this.setState({ x: this.state.x - xDiff, y: this.state.y - yDiff, }); }
用于调整矩形大小的缩放器事件:
onMouseDown = (e) => { document.addEventListener('mousemove', this.onMouseMove); this.props.updateStateResizing(this.props.id, true); this.props.updateStateDragging(this.props.id, false); } onMouseMove = (e) => { if ( this.props.isResizing ){ this.props.nodeResizer(this.props.id, e.target, e.clientX, e.clientY); } } onMouseUp = (e) => { document.removeEventListener('mousemove', this.onMouseMove.bind(this)); if ( this.props.isResizing ){ this.props.updateStateResizing(this.props.id, false); } }
我究竟做错了什么?怎么解决呢?
MDSN
MDSN
青春有我
偶然的你
相关分类