jquery touchstart in browser

后端 未结 3 1420
一生所求
一生所求 2020-12-09 17:06

As touchstart/touchend is yet not supported in most of the desktop browsers. How can I create touchstart event that will be the same as mousedown event (that is supported in

3条回答
  •  再見小時候
    2020-12-09 17:41

    It's not the nicest solution, but the best I found till now. Downfall? It only works after first touch happened so you cannot use synchronously :(

     var isTouch = false;
     $('body').on('touchstart', function () {
        isTouch = true;
     });
    

    You can also check if browser support touch first with modernizr for example.

    Remember to don't use it in global scope (unless you have to). You can also change it to be modernizr "alike" by adding is-touch-device class to Html tag by adding after isTouch = true code: $('html').addClass('is-touch-device'). In this case you can reference it from everywhere (from css also).

提交回复
热议问题