Touch event on iOS
If a webview is embedded in an iOS App, and the webview's size is only a portion of the screen, then when a finger swipes in the webview, the swipe may be too fast and go beyond the webview window; at this point, if the finger leaves the screen, inside the webview, the
touchend event will not be triggered.The code will remain stuck on the touchmove event.The solution is to, on
touchmove , set up a timeout to automatically trigger thetouchend event.But there is an even more magical thing: if the finger stays within the webview window, at this point, through
new TouchEvent('touchend') , it is possible to manually create an event.But when the finger moves out of the webview window,
new TouchEvent('touchend') will report an error:'[object EventConstructor]' is not a constructorThe solution is to add compatibility handling:
var event = null;
try {
event = new TouchEvent('touchend');
} catch (e) {
event = document.createEvent('Event');
event.initEvent('touchend', true, true);
}The above can then successfully create an event manually, and then through
eventTarget.dispatchEvent(event) to manually trigger the event.
