实现原理是将气泡弹窗设置为绝对定位,根据券码的位置来计算气泡弹窗相对于父米素的位置,其中父米素为每一条item的包裹米素coupon-code-item
本次的实现难点是需要自己手动定义vue的长按点击事件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | Vue.directive('tap',{ bind:function(el,binding){ var startTx, startTy,endTx,endTy,longClick,timeOutEvent, longMethod=binding.value.longMethod, method = binding.value.method, params = binding.value.params, propagation=binding.value.propagation; el.addEventListener(touchstart,function(e){ var touch=e.touches[0]; startTx = touch.clientX; startTy = touch.clientY; if(longMethod && typeof longMethod==='function'){ longClick=0; timeOutEvent =setTimeout(function(){ longClick=1; longMethod(e, params); },500) } if(!propagation){ if (e.stopImmediatePropagation) { e.stopImmediatePropagation(); } else { e.propagationStopped = true; } } },false ); el.addEventListener(touchmove,function(e){ if(longMethod && timeOutEvent){ clearTimeout(timeOutEvent); timeOutEvent = 0; } },false); el.addEventListener(touchend,function(e){ var touch = e.changedTouches[0]; endTx = touch.clientX; endTy = touch.clientY; if(longMethod && timeOutEvent){ clearTimeout(timeOutEvent); } if((timeOutEvent!=0 && longClick==0)||!longMethod){ if( Math.abs(startTx - endTx) < 6 && Math.abs(startTy - endTy) < 6){ if(params instanceof Array){ method(...params); } else method(params); } var focusInput=document.querySelector('input:focus'); if(focusInput)focusInput.blur(); var tagName=el.tagName.toLowerCase(); if(tagName==='input'||tagName==='textarea')el.focus();//获取焦点 if(!propagation){ if (e.stopImmediatePropagation) { e.stopImmediatePropagation(); } else { e.propagationStopped = true; } e.stopPropagation(); return false; } } },false); } }) |
Android实现:
Android原生实现起来比较简单,直接贴代码