Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

桌面浏览器支持测试touch事件 #2

Open
chemzqm opened this issue Nov 7, 2015 · 2 comments
Open

桌面浏览器支持测试touch事件 #2

chemzqm opened this issue Nov 7, 2015 · 2 comments

Comments

@chemzqm
Copy link

chemzqm commented Nov 7, 2015

桌面浏览器不支持 createTouch 方法,Chrome 开启模拟也会报错:

Uncaught TypeError: document.createTouch is not a function

但是可以创建 UIEvent 来模拟 touch 事件

    var t = document.getElementById('t');
    t.addEventListener('touchstart',function(e){
      console.log(e.touches)
    },false);

    function fire (element, event) {
      var e = new UIEvent(event, {
          bubbles: true,
          cancelable: false,
          detail: 1
      })
      e.touches = [{pageX: 0, pageY: 0}]
      element.dispatchEvent(e);
    }
    fire(t, 'touchstart')

Chrome firefox 和 safari 都会触发 touchstart 事件,IE不确定,组件里面应该可以做到降级处理

@yiminghe
Copy link
Owner

欢迎 pr

@chemzqm
Copy link
Author

chemzqm commented Nov 11, 2015

这项目里面代码有点太复杂,我搞了一个小组件 https://github.com/chemzqm/touch-simulate 来测试touch事件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants