onclick and ontouchstart simultaneously

后端 未结 6 1926
花落未央
花落未央 2021-02-08 21:23

I have an ontouchstart event triggered on my mobile view, its linked to this:

function mobileLinksShow() {
    document.querySelector(\'.mobile-head         


        
6条回答
  •  失恋的感觉
    2021-02-08 21:49

    This is still broken for cases like a touchscreen laptop (where 'click' may be from touch or from a mouse). Its also broken in some mobile cases where click doesn't come from touch (eg. Enter key on an attached or bluetooth keyboard).

    The right solution is to correctly mark the touch event as handled with preventDefault, then no click event will be generated. Eg.

    function mobileLinksShow(event) {
      document.querySelector('.mobile-head-bar-links').classList.toggle('mobile-head-bar-links-transition');
      event.preventDefault();
    }
    

    See http://www.html5rocks.com/en/mobile/touchandmouse/ for details.

提交回复
热议问题