jquery如何利用mousedown、mousemove和mouseup等事件来实现手势识别

2024年06月04日 建站教程

jquery开发中如何利用mousedownmousemovemouseup等事件来实现手势识别,下面web建站小编给大家简单介绍一下具体实现代码!

具体代码如下:

var startX, startY, endX, endY;
 
$(document).on('mousedown', function(e) {
  startX = e.clientX;
  startY = e.clientY;
});
 
$(document).on('mousemove', function(e) {
  endX = e.clientX;
  endY = e.clientY;
 
  var direction = swipeDirection(startX, startY, endX, endY);
});
 
$(document).on('mouseup', function(e) {
  endX = e.clientX;
  endY = e.clientY;
 
  var direction = swipeDirection(startX, startY, endX, endY);
});
 
function swipeDirection(startX, startY, endX, endY) {
  var diffX = Math.abs(startX - endX);
  var diffY = Math.abs(startY - endY);
 
  if (diffX > diffY) {
    return (startX > endX) ? 'left' : 'right';
  } else {
    return (startY > endY) ? 'up' : 'down';
  }
}

本文链接:http://so.lmcjl.com/news/5929/

展开阅读全文
相关内容