26

I'd like to make it so that as you drag your finger over a set of elements, the background of the one you've got your finger on changes.

It seems like I want to use the touchmove event for this, but as far as I can tell the target element never changes, even as you drag around. The clientX and clientY do change, and I found this document.elementFromPoint function that works in chrome, but seems very roundabout (plus I'm not sure which browsers support it)

See this fiddle, I want the boxes to turn green as you touch through them:

http://jsfiddle.net/QcQYa/9/

By the way, in chrome, you'll need to go into the user agent tab of the inspector config modal and choose "emulate touch events" to see my example.

Edit: I found an idea to use mouseenter here How to detect html elements on touchmove and got it to work on desktop chrome, but not on mobile safari.

1
  • You have to look at the coordinates of your touchmove event and see if they overlap with the coordinates of your html element. I had to write that same code for a jquerymobile android game, I dont have it here at home but tomorrow can look it up and update your fiddle with it.
    – Nelson
    Sep 12, 2012 at 22:17

3 Answers 3

25

I took a different approach:

Every touch event I check if the touch position is inside a $('.catch') object.

function highlightHoveredObject(x, y) {
    $('.catch').each(function() {
      // check if is inside boundaries
      if (!(
          x <= $(this).offset().left || x >= $(this).offset().left + $(this).outerWidth() ||
          y <= $(this).offset().top  || y >= $(this).offset().top + $(this).outerHeight()
      )) {

        $('.catch').removeClass('green');
        $(this).addClass('green');
      }
    });
}

You can see it working on jsFiddle.
It works on Chrome, I hope it also does on mobile devices.

Edit:
In this fiddle I used both versions, mine and that one from the link in the comments (document.elementFromPoint – a jQuery solution), both seem to work on my Android phone. I also added a quick and dirty benchmark (see console) and as expected document.elementFromPoint is a few thousandth faster, if that is your concern you should check the support of document.elementFromPoint for the browsers you want to support.

4
  • 1
    seems to work, care to weigh in on this vs document.elementFromPoint?
    – spike
    Sep 12, 2012 at 23:02
  • Well certainly the browser incompatibility is the biggest drawback. I found 2 articles which might be in your very interest: document.elementFromPoint – a jQuery solution and elementFromPoint() under iOS 5. There you also see you have to calc the current viewport. I am not sure if the same applies to my solution. For me my solution looks less pain but it's up to you what to choose
    – dan-lee
    Sep 13, 2012 at 5:49
  • What I didn't think of is that you need to address phone/tablet browsers. Unfortunately I don't know which mobile browsers support document.elementFromPoint. But I think if they all support them it would be a slight performance increase because internal functions happen to be faster.
    – dan-lee
    Sep 13, 2012 at 7:02
  • 2
    Thanks for the extra advice. document.elementFromPoint works on iOS, but I'm going to use a modified version of your solution. After testing it out on my phone, it makes sense to allow the user's finger to drift slightly above or below the boxes as he slides left/right. The app store's ratings widget does this, for instance, and it makes it feel much nicer. I can't handle that with document.elementFromPoint.
    – spike
    Sep 13, 2012 at 14:38
2

You'll find my solution in this fiddle , I tested it on my android phone and it works fine, it uses jquerymobile to take advantage of vmousemove event, it also attachs a handler to touchmove event just to prevent scrolling the browser view on the mobile device.

I paste here the relevant HTML and javascript bits:

<div id="main" ontouchmove="touchMove(event);">
   <span class='catch'>One</span>
   <span class='catch'>Two</span>
   <span class='catch'>Three</span>
   <span class='catch'>Four</span>
   <span class='catch'>Five</span>
   <span class='catch'>Six</span>
   <span class='catch'>Seven</span>
</div>

now the javascript:

function elem_overlap(jqo, left, top) {
   var d = jqo.offset();
   return top >= d.top && left >= d.left && left <= (d.left+jqo[0].offsetWidth)
          && top <= (d.top+jqo[0].offsetHeight);
}

/* To prevent WebView from scrolling on swipe, see http://goo.gl/DIZbm */
touchMove = function(event) {
   event.preventDefault(); //Prevent scrolling on this element
}

$("#main").bind("vmousemove", function(evt){
   $('.catch').each(function(index) {
      if ( elem_overlap($(this), evt.pageX, evt.pageY) ) {
         $('.catch').not('eq('+index+')').removeClass('green');
         if (!$(this).hasClass('green')) {
            $(this).addClass('green');
         }
      }
   });
});
1
  • Thanks for this different solution. I'm not using jquery mobile yet, but this seems to work well.
    – spike
    Sep 13, 2012 at 14:40
1

You can't "trigger a touchend event" or cancel touches, which you would need to start touching another.

So you would be better off binding the touchmove event to the container, and manipulating the boxes based on their position/sizes and the touch position, like what part of Dan Lee's answer does.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.