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
| // jQuery 代码 var isDragging = false, element = $('#element'); element.on('touchstart', '.target', function(event) { isDragging = false; }); element.on('touchmove', '.target', function(event) { isDragging = true; }); element.on('touchend', '.target', function(event) { if(isDragging) return false; var self = $(this); // 点击时的业务代码 console.log(self); });
// 原生 JS 代码(ES5) var isDragging = false, element = document.querySelector('#element'); element.addEventListener('touchstart', function(event) { isDragging = false; }, false); element.addEventListener('touchmove', function(event) { isDragging = true; }, false); element.addEventListener('touchend', function(event) { if(isDragging) return false; var self = event.target, t = 'target'; while(self && self.classList && !self.classList.contains(t)){ self = self.parentNode; } // 点击时的业务代码 console.log(self); }, false);
|