manually trigger touch event

前端 未结 5 1048
甜味超标
甜味超标 2020-12-05 02:55

I searched for the past 30 minutes, but didn\'t find a solution.

I want to trigger a touchstart event on an element.

This fires the touchs

5条回答
  •  一向
    一向 (楼主)
    2020-12-05 03:31

    In 2019, we can use TouchEvent and Touch.

    Touch is an experimental technology

    For example,

    const touch = new Touch({
      identifier: "123",
      target: target,
    });
    
    const touchEvent = new TouchEvent("touchstart", {
      touches: [touch],
      view: window,
      cancelable: true,
      bubbles: true,
    });
    
    target.dispatchEvent(touchEvent);
    

    I created gist. try it simple.

提交回复
热议问题