美文网首页
JS模拟Touch事件

JS模拟Touch事件

作者: oaiMZX苗 | 来源:发表于2019-08-07 12:13 被阅读0次

    当前在浏览器console模式下无法采用和click一样的方式touch某个节点,当前最新触发touch事件的方式如下:

            var ele = $('Test');
            var rect = ele.getBoundingClientRect();
            var touch = new Touch({
                "identifier" : 0,
                "target" : ele,
                "clientX" : rect.x + rect.width/2,
                "clientY" : rect.y + rect.height/2,
                "screenX" : rect.x + rect.width/2,
                "screenY" : rect.x + rect.width/2,
                "pageX" : rect.x + rect.width/2,
                "pageY" : rect.x + rect.width/2,
                "radiusX" : 11.5,
                "radiusY" : 11.5,
                "rotationAngle" : 0.0,
                "force" : 1});
            
            var touchstart = new TouchEvent("touchstart", {
                cancelable: true,
                bubbles: true,
                composed: true,
                touches: [touch],
                targetTouches: [touch],
                changedTouches: [touch]
            });
            
            var touchend = new TouchEvent("touchend", {
                cancelable: true,
                bubbles: true,
                composed: true,
                touches: [touch],
                targetTouches: [touch],
                changedTouches: [touch]
            });
            
            ele.dispatchEvent(touchstart);
            ele.dispatchEvent(touchend);
    

    相关文章

      网友评论

          本文标题:JS模拟Touch事件

          本文链接:https://www.haomeiwen.com/subject/ihkodctx.html