场景:(见代码)
addBodyMove(){
this.bodyDom.addEventListener('touchmove',this.bodyMove.bind(this),{ passive: false })
}
addBodyMoveEnd(){
this.bodyDom.addEventListener('touchend',this.bodyMoveEnd.bind(this),{ passive: false })
}
removeBodyMove(){
this.bodyDom.removeEventListener('touchmove',this.bodyMove)
}
removeBodyMoveEnd(){
this.bodyDom.removeEventListener('touchend',this.bodyMoveEnd)
}
touchmove 事件一直无法 removeEventListener;
问题解决:
.bind()方法会返回新一个新函数,并不是之前的函数了。
将.bind()返回的新函数 重新赋值给 旧函数 ,如下
addBodyMove(){
this.bodyMove = this.bodyMove.bind(this);
this.bodyDom.addEventListener('touchmove',this.bodyMove,{ passive: false })
}
addBodyMoveEnd(){
this.bodyMoveEnd = this.bodyMoveEnd.bind(this)
this.bodyDom.addEventListener('touchend',this.bodyMoveEnd,{ passive: false })
}
removeBodyMove(){
this.bodyDom.removeEventListener('touchmove',this.bodyMove)
}
removeBodyMoveEnd(){
this.bodyDom.removeEventListener('touchend',this.bodyMoveEnd)
}