Mousedown Event with ipad/iphone Compatibility? -

2020-07-18 07:26发布

I wrote a small scroller in jquery. The scroller seems to work perfectly on PC and Macs. However it doesn't work on touch devices.

I guess this is due to mousedown property being called. how do I make this work on both PC and touch screen devices?

Thanks

$('.scroll-nav-up, .scroll-nav-down').live('click', function(){
    $('.scroller-wrap').stop();
    return false;
});


$('.scroll-nav-up').live('mousedown', function(){

    $(this).closest('.item').find('.scroller-wrap').animate({
        top: -($(this).closest('.item').find('.scroller-wrap').height() - 250)
    }, 800);

});


$('.scroll-nav-down').live('mousedown', function(){

    $(this).closest('.item').find('.scroller-wrap').animate({
        top: 0
    }, 800);

});

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-07-18 08:05

Nothing special found, so at this moment I am using 2 scripts. The first script identifies the the touch screen devices as written by Alastair Campbell on Detecting touch-based browsing.

function isTouchDevice() {
   var el = document.createElement('div');
   el.setAttribute('ongesturestart', 'return;');
   if(typeof el.ongesturestart == "function"){
      return true;
   }else {
      return false
   }
}

and then use the mousedown event for regular browsers and use tap event for touch screen devices.

P.S - Using both jQuery and jQuery Mobile

查看更多
家丑人穷心不美
3楼-- · 2020-07-18 08:07

If you're using jquery mobile try handling its new events tap and taphold

http://jquerymobile.com/demos/1.0a2/#docs/api/events.html

I can't test it, so it's just an idea (I'm not sure if I understand what your app does), but you can try and change click to mouseup event.

查看更多
登录 后发表回答