衆所周知,安卓手機上touch事件一直有各類各樣莫名其妙的問題。html
$('body').bind("touchmove", function(e) { e.preventDefault(); });
$(id).bind("touchmove", function(e) { e.stopPropagation() });
var startX, startY; var endX, endY; var distanceX, distanceY; $('body').bind('touchstart', function(event) { startX = event.targetTouches[0].clientX; startY = event.targetTouches[0].clientY; }).bind('touchend', function(event) { endX = event.changedTouches[0].clientX; endY = event.changedTouches[0].clientY; distanceX = Math.abs(startX - endX); distanceY = Math.abs(startY - endY); if (distanceX > distanceY) { startX - endX > 0 ? swipeLeft() : swipeRight(); } });
var count = 0; //判斷用戶是否第一次進行touchmove操做 var startX, startY; var endX, endY; var distanceX, distanceY; $('body').bind('touchstart', function(event) { count = 0; //每次開始點擊時清零 startX = event.targetTouches[0].clientX; startY = event.targetTouches[0].clientY; }).bind('touchmove', function(event) { if (count === 0) { //若是是第一次滑動 endX = event.changedTouches[0].clientX; endY = event.changedTouches[0].clientY; distanceX = Math.abs(startX - endX); distanceY = Math.abs(startY - endY); if (distanceX > distanceY) { //若是X絕對距離大於Y絕對距離 event.preventDefault(); } } count++; }).bind('touchend', function(event) { endX = event.changedTouches[0].clientX; endY = event.changedTouches[0].clientY; distanceX = Math.abs(startX - endX); distanceY = Math.abs(startY - endY); if (distanceX > distanceY) { startX - endX > 0 ? swipeLeft() : swipeRight(); } });