7

I've gone through the suggested threads but appears this has not been posted before. The closest one is this but it requires to know the coordinates: Detect finger drag using Javascript on touch phones?

Suppose I have 3 <div style='float: left'></div> pairs:

<div id='Adiv' style='float: left'>A</div>
<div id='Bdiv' style='float: left'>B</div>
<div id='Cdiv' style='float: left'>C</div>

Instead of an onClick event on the divs, I want to detect how the user interacts with the buttons.

For example, if the user places a finger on A, then drags to B then drags to C, I want to output : ABC

And if the user places a finger on B, then drags to A then drags to C without lifting, I want to output : BABC.

Basically I would like to detect if a finger has moved/swiped his finger over a DIV then I would like to know it. Is this even possible?

Thanks for your guidance.

p/s This is for the mobile web browser, btw. Best,

Community
  • 1
  • 1
Azrudi
  • 101
  • 1
  • 7

1 Answers1

4

It's a little bit tricky because you have no touchover event or similar.

So the solution is to detect the "touchover" by the elements "coords".

  1. Wrap them with div (for example) and listen to him touchmove event.
  2. Store the "coords" of the children (performance).
  3. When touchmove on wrapper, get the x,y values from the event.
  4. Check who from the children is in those values.

Now, to the code

// first - store the coords of all the cells for the position check
var matrix = $('.wrapper div').map(function() {
  var e = $(this),
      o = e.offset(),
      w = e.width(),
      h = e.height();

  return {
    top: o.top,
    left: o.left,
    right: o.left + w,
    bottom: o.top + h,
    e: e
  }
}).get();

var currentTarget = $(),
    activeTarget = $();


var touchF = function(e) {
  var touch = e.originalEvent.touches[0];
  currentTarget = getCurrent(
    {
      clientX: touch.clientX,
      clientY: touch.clientY
    }
  );

  // if the touch is in one of the cells and it's disfferent than the last touch cell
  if (currentTarget && currentTarget != activeTarget) {
    activeTarget = currentTarget;
    console.log(currentTarget.html());
    $('#output').append(currentTarget.html() + ' ');
  }
} 

$('.wrapper').bind({
  touchstart: touchF,
  touchmove: touchF
});

function getCurrent(touch) {
  // check if the touch coords are in the position of one of the cells and which one
  var a = matrix.filter(function(obj) {
    var b = (
      touch.clientX > obj.left &&
      touch.clientX < obj.right &&
      touch.clientY < obj.bottom &&
      touch.clientY > obj.top
    );

    return b;
  });

  return a.length > 0 ? a[0].e : null;
}
.wrapper:after {
  content:" ";
  display:table;
  clear:both;
}

.wrapper div {
  width:50px;
  height:50px;
  border:1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div id='Adiv' style='float: left'>A</div>
  <div id='Bdiv' style='float: left'>B</div>
  <div id='Cdiv' style='float: left'>C</div>
</div>
<hr />
<div id="output"></div>

http://jsbin.com/kokoxuwebi/edit?html,css,js

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • Thank you, it seems ti doesn't work in a desktop browser? I changed the console.log to window.alert() for now but nothing is popping up? – Azrudi Mar 01 '16 at 03:23
  • Of course it doesn't. You need to run different code for desktop and mobile. Do you know how to do this? – Mosh Feu Mar 01 '16 at 05:36
  • Oh okay, I didn't know that. But I intended for it to run it on a mobile anyway, so don't worry about the desktop version. Thanks =) I did push it up on the web to test it with my mobile browser, I could not see what was happening because I couldn't open a console window on the mobile to see if its working. I needed to see the response, should I open a field to see if its logging? Right now it doesn't seem as if anything is happening and I wondered if I did it wrong. I made 3 files, the html, css and js. I put – Azrudi Mar 01 '16 at 12:17
  • I was updated my answer so now you can see the `console` output on the screen. – Mosh Feu Mar 01 '16 at 14:28
  • Thank you for all your effort, but it doesn't quite work, I can tap the A B C divs but if I swipe over them nothing is outputted. Please view: www.nusantara.com.my/matsMate/swipe.html , www.nusantara.com.my/matsMate/swipe.js , www.nusantara.com.my/matsMate/swipe.css – Azrudi Mar 01 '16 at 17:19
  • GREAT! Thanks! Worked on Chrome for Android and Windows (desktop). Spent hours looking for something like this! – Tomas Gonzalez Dec 16 '17 at 02:35