jquery click event for tablets and touch screen phones

*爱你&永不变心* 提交于 2021-02-04 10:03:22

问题


Ok, I am fairly new to web development for mobile/tablet platforms. I am pretty confident though in developing for a browser environment.

My problem is that I don't own any tablet (I am poor so have to fly blind) so it's hard for me to do testing. I'm also quite anxious about the click events not working properly.

How can I ensure that click and mousedown jquery events work on tablets/smartphones correctly?

Is there any consensus on best practice for click events and tablets/smartphones?

Note: I am not developing a mobile only site - the site transforms shape for mobile.


回答1:


Mobile browsers will still respond to click events, though they will introduce a delay (usually around 300 ms). If you want a more responsive experience, you can try to detect if you are on a mobile platform and use either touchstart or touchend events. I find touchend is usually a better experience, mainly because the user's hand is still in the way when touchstart fires.

Something like this:

$(element).on(isMobile ? 'touchend' : 'click', function(e) {...});

Or you can use hammer.js, which will work for both desktop and mobile.

As for testing, you can read through the answers to this Stackoverflow question: Simulating touch events on a PC browser.



来源:https://stackoverflow.com/questions/17068877/jquery-click-event-for-tablets-and-touch-screen-phones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!