標籤:
在我們滑動手機的時候,如果LI或者DIV標籤可以點擊,那麼在移動端給使用者一個效果
/*id為添加效果LI上的UL的ID,或者是當前DIV的ID*/function doTouchPublic(id){ setTimeout(function(){ var obj=document.getElementById(id); touchPublic.tagUltagDiv(obj); },300); };var touchPublic={ tagUltagDiv:function(obj){ var objAll; /*如果對象為UL,那麼給LI添加事件,否則給DIV當前對象添加事件*/ if(obj.tagName=="UL"){ objAll=obj.childNodes; }else{ objAll=obj; } touchPublic.touchMoveEndIf(objAll); }, /*li做委託事件,on為JQ中委託事件*/ touchMoveEndIf:function(obj){ $(obj).on(‘touchstart‘,function(){ touchPublic.touchObjstart(this,"objRowOver"); }) /*觸摸滑動,e.preventDefault();是為了讓end觸發*/ $(obj).on(‘touchmove‘,function(){ touchPublic.touchObjend(this,"objRowOver"); }); /*觸摸離開*/ $(obj).on(‘touchend‘,function(){ touchPublic.touchObjend(this,"objRowOver"); }); }, touchObjstart:function(that,color){ $(that).addClass(color); }, touchObjend:function(that,color){ if($(that).attr(‘class‘)==color){ $(that).removeClass(color); } }}
手機端touchstart,touchmove,touchend事件,最佳化使用者劃入某個可以點擊LI的效果