項(xiàng)目的需求是給五個(gè)元素先添加一個(gè)動(dòng)畫,在動(dòng)畫執(zhí)行完成后再給五個(gè)元素添加另外一個(gè)動(dòng)畫。元素以數(shù)組的形式保存在了arr中,實(shí)際在執(zhí)行的過程中發(fā)現(xiàn)section.removeEventListener("animationend",down.bind(this,arr,section),false)這一句沒有正常生效,在for循環(huán)執(zhí)行完成后,會(huì)反復(fù)再次執(zhí)行down(arr,section)這個(gè)方法。中間插入clear()這個(gè)方法的執(zhí)行結(jié)果。判斷是在section這個(gè)元素上“animationend”執(zhí)行完成后,同時(shí)綁定了down()和clear()兩個(gè)方法的原因?qū)е碌摹,F(xiàn)在問題就是不理解為什么removerEventListener()這個(gè)方法會(huì)不生效//?給元素的父級(jí)元素添加監(jiān)聽animationend事件,避免元素結(jié)束動(dòng)畫時(shí)間不一致導(dǎo)致重復(fù)執(zhí)行事件
????????//?使用bind方法綁定執(zhí)行環(huán)境且傳遞兩個(gè)數(shù)組和父元素
????????section.addEventListener("animationend",down.bind(this,arr,section),false)
????????//?var?bb=down.bind(this,arr,section,bb)
????????//?定義方法,在上升動(dòng)畫執(zhí)行完畢后觸發(fā)。
????????function?down(arr,section){
????????????//?console.log(le)
????????????console.log(section)
????????????//?刪除父級(jí)元素中綁定的動(dòng)畫結(jié)束后執(zhí)行的方法
????????????section.removeEventListener("animationend",down.bind(this,arr,section),false)
????????????//?var?kk=down.bind(this,arr,section,bb)
????????????????//?var?le=arr[i]
????????????????//?console.log(kk==bb)
????????????????//?遍歷數(shù)組,為元素綁定下降的動(dòng)畫
????????????console.log(arr[1].style.animationName)
????????????????for(var?j=0;j<arr.length;j++){
????????????????????console.log(arr[j].style.animationName)
????????????????????arr[j].style.animation="bb?1s"
????????????????????console.log(arr[j].style.animationName)
????????????????????arr[j].style.animationTimingFunction="cubic-bezier(0.25,0.5,0.75,1)"
????????????????//?le.style.animationFillMode="forwards"
????????????????console.log(arr[j])
????????????????}
????????????//?監(jiān)聽父級(jí)元素,在所有元素下降動(dòng)畫執(zhí)行完畢后,執(zhí)行清除方法,清除元素的動(dòng)畫
????????????section.addEventListener("animationend",clear.bind(this,arr),false)
使用addEventListener在函數(shù)體外綁定的函數(shù),在函數(shù)體內(nèi)無法取消綁定
心為形役
2017-10-26 09:41:26