Crossing over to new elements during touchmove

后端 未结 3 700
独厮守ぢ
独厮守ぢ 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 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:

    One Two Three Four Five Six Seven

    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');
             }
          }
       });
    });
    

提交回复
热议问题