简体   繁体   中英

mouse up - click outside of Div not working on mobile

I use the following to hide a div and ovelay div when clicking outside a form.

The following javascript is:

$(document).mouseup(function (e)
{
  var container = $("#feedbackform");
  var overlay = $("#overlay");

  if (!container.is(e.target) // if the target of the click isn't the container...
    && container.has(e.target).length === 0) // ... nor a descendant of the container
  {
    $('#feedbackform').fadeOut('fast'),
    $('#overlay').fadeOut('fast');
  }
});

This works fine on a desktop but not on touch mobile devices.

Im guessing its to do with mouseup, any suggestions?

Craig.

Classic events won't work exactly the same on mobile devices (except some events such as 'click' which is quite the same).

So now you have some mobile specific events named touch events : https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Touch_events

So touchend is probably the one you want to use :

$(document).on('mouseup touchend', function (e){

[...]

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM