2 回答
TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
|
只有被addEventListener方法添加的事件才可以使用removeEventListener來注銷.
刪除事件時(shí)removeEventListener的三個(gè)參數(shù)必須要跟addEventListener的參數(shù)相同
語法:elem.removeEventListener(event_type,fun_name,bool);
event_type:事件類型.比如單擊,或雙擊.或移動(dòng)鼠標(biāo)事件等.
fun_name:要被注銷的事件名稱,函數(shù)名.
bool:布爾值.true或false.true代表支持事件冒泡.false則不支持事件冒泡捕獲
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
function PhoneDrag(id) { var _this = this; this.oDiv = document.getElementById(id); this.disX = 0; this.disY = 0; this.oDiv.addEventListener("touchstart", function (ev) { _this.fnDown(ev); }, false); }PhoneDrag.prototype.fnDown = function (ev) { var _this = this; var oEvent = ev.touches[0]; ev.preventDefault(); this.disX = oEvent.clientX - this.oDiv.offsetLeft; this.disY = oEvent.clientY - this.oDiv.offsetTop; // 解除事件綁定的時(shí)候需要用到 var touchmoveHandle = function (ev) { _this.fnMove(ev); }; document.addEventListener("touchmove", touchmoveHandle, false); document.addEventListener("touchend", function (ev) { //這里是手指抬起的時(shí)候 如何刪除touchmove事件 document.removeEventListerner('touchmove', touchmoveHandle, false); }, false); }PhoneDrag.prototype.fnMove = function (ev) { var oEvent = ev.touches[0]; var iX = oEvent.clientX - this.disX; var iY = oEvent.clientY - this.disY; this.oDiv.style.top = iY + "px"; this.oDiv.style.left = iX + "px";} window.onload = function () { new PhoneDrag("div1");} |
- 2 回答
- 0 關(guān)注
- 168 瀏覽
添加回答
舉報(bào)
