javascript - How to manually trigger a TouchEvent in TypeScript - Stack Overflow

I want to trigger the touch event manually but i can't able to call.this was possible in JavaScri

I want to trigger the touch event manually but i can't able to call. this was possible in JavaScript but TypeScript can't able to achieve.

Example:

 let touchStart: TouchEvent = document.createEvent('TouchEvent');
 touchStart.initEvent('touchstart', true, true);
 element.dispatchEvent(touchStart);

If I tried this I can able to trigger touchStart event but I can't able to get the changedTouches[0].pageX and changedTouches[0].pageY

based on this link i have tried like as below,

 touchStart.changedTouches = [
    pageX: x
    pageY: y
  ]

But I can't able to assign the value in changedTouches because of that property is readonly.

How to achieve this? with changedTouches in TypeScript?

I want to trigger the touch event manually but i can't able to call. this was possible in JavaScript but TypeScript can't able to achieve.

Example:

 let touchStart: TouchEvent = document.createEvent('TouchEvent');
 touchStart.initEvent('touchstart', true, true);
 element.dispatchEvent(touchStart);

If I tried this I can able to trigger touchStart event but I can't able to get the changedTouches[0].pageX and changedTouches[0].pageY

based on this link i have tried like as below,

 touchStart.changedTouches = [
    pageX: x
    pageY: y
  ]

But I can't able to assign the value in changedTouches because of that property is readonly.

How to achieve this? with changedTouches in TypeScript?

Share Improve this question edited Mar 3, 2017 at 13:53 Madara's Ghost 175k50 gold badges272 silver badges314 bronze badges asked Mar 3, 2017 at 9:38 Akbar BashaAkbar Basha 1,1981 gold badge17 silver badges40 bronze badges 3
  • 1 Very interestingly, it seems as though TypeScript doesn't properly support the creation of TouchEvents, neither through new TouchEvent nor through the older document.createEvent(). I'd file a bug but I'm a bit short of time at the moment. – Madara's Ghost Commented Mar 3, 2017 at 10:05
  • @MadaraUchiha it is a bug? – Akbar Basha Commented Mar 3, 2017 at 10:53
  • 1 Yes, it looks like a real bug in TypeScript. – Madara's Ghost Commented Mar 3, 2017 at 10:54
Add a ment  | 

2 Answers 2

Reset to default 2

This is an actual bug with TypeScript's lib.d.ts which defines how the types of everything in JavaScript and the DOM look like.

I've opened a ticket and a pull request to fix this issue.

If they get accepted, you can expect the standard way of new TouchEvent(eventType, initArguments); to work in future versions of TypeScript.

See this MDN tutorial for the correct way of doing it in JavaScript.

I have recently been trying to trigger touch events in TypeScript, and hit upon the same issues. Looking into it, I saw how the Angular Material team got around this in their tests, so I took a similar approach. Below is example of the code I'm using to mock TouchEvents with touchstart and the changedTouches mock array being set:

let te = createTouchEvent(parentHtmlElement, 'touchstart');
parentHtmlElement.dispatchEvent(te);

function createTouchEvent(touchTarget: HTMLElement, type: string) {        
    const event = document.createEvent('UIEvent');    
    event.initUIEvent(type, true, true, window, 0);

    let touches = [{target: touchTarget}];

    Object.defineProperties(event, {
      changedTouches: {value: touches}
    });    
    return event;
}

In my ponents I am listening for the events as follows:

 this.touchStartListener = this.renderer.listen('document', 'touchstart', (event: TouchEvent) => {
                    this.handleTouchStart(event);
                });

This works very well, no TypeScript warnings, and the events are triggered and handled in my tests

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745349399a4623738.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信