标签:art img div tar 项目 function target splay http
实现功能:左滑列表项(<li class="route-item" ></li>),出现删除按钮(<div class="removeJs">删除</div>)。
js部分的代码如下:
1 window.addEventListener(‘load‘,function(){ 2 var initX; 3 var moveX; 4 var X = 0; 5 var objX = 0; 6 var width=$(".removeJs").width();//按钮的宽度(removeJs修改为按钮的className) 7 var liClassName=".route-item";//li的className(route-item修改为li的className) 8 $("body").on("touchstart",liClassName,function (event) { 9 var obj=this; 10 initX = event.targetTouches[0].pageX; 11 objX =(obj.style.WebkitTransform.replace(/translateX\(/g,"").replace(/px\)/g,""))*1; 12 if( objX == 0){ 13 $("body").on(‘touchmove‘,liClassName,function(event) { 14 // event.preventDefault(); 15 var obj=this; 16 moveX = event.targetTouches[0].pageX; 17 X = moveX - initX; 18 if (X > 0) { 19 obj.style.WebkitTransform = "translateX(" + 0 + "px)"; 20 } 21 else if (X < 0) { 22 var l = Math.abs(X); 23 obj.style.WebkitTransform = "translateX(" + -l + "px)"; 24 if(l>width){ 25 l=width; 26 obj.style.WebkitTransform = "translateX(" + -l + "px)"; 27 } 28 } 29 }); 30 } 31 else if(objX<0){ 32 $("body").on("touchmove",liClassName,function (event) { 33 // event.preventDefault(); 34 var obj=this; 35 moveX = event.targetTouches[0].pageX; 36 X = moveX - initX; 37 if (X > 0) { 38 var r = -width + Math.abs(X); 39 obj.style.WebkitTransform = "translateX(" + r + "px)"; 40 if(r>0){ 41 r=0; 42 obj.style.WebkitTransform = "translateX(" + r + "px)"; 43 } 44 } 45 else { //向左滑动 46 obj.style.WebkitTransform = "translateX(" + -width + "px)"; 47 } 48 }); 49 } 50 }) 51 $("body").on("touchend",liClassName,function (event) { 52 var obj=this; 53 objX =(obj.style.WebkitTransform.replace(/translateX\(/g,"").replace(/px\)/g,""))*1; 54 if(objX>-width/2){ 55 obj.style.WebkitTransform = "translateX(" + 0 + "px)"; 56 }else{ 57 obj.style.WebkitTransform = "translateX(" + -width + "px)"; 58 } 59 }) 60 })
ps:实际项目中,只需修改如下两行代码即可:
var width=$(".removeJs").width();
var liClassName=".route-item";
标签:art img div tar 项目 function target splay http
原文地址:http://www.cnblogs.com/ishuanghe/p/7989858.html