Crossing over to new elements during touchmove

后端 未结 3 680
独厮守ぢ
独厮守ぢ 2020-12-08 10:27

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 u

相关标签:
3条回答
  • 2020-12-08 10:55

    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.

    0 讨论(0)
  • 2020-12-08 11:15

    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');
             }
          }
       });
    });
    
    0 讨论(0)
  • 2020-12-08 11:16

    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.

    0 讨论(0)
提交回复
热议问题