I have some websites I built times ago, that use jquery mouse events...I just got an ipad and i noticed that all the mouse over events are translated in clicks...so for instance i have to do two clicks instead of one..(the first hover, than the actual click)
is there a workaround ready to solve this? maybe a jquery command i shoudl have used instead of mouseover/out etc.. thanks!
I am too late, I know but this is one of the easiest workaround I've found:
This does not only works for links(anchor tags) but for other elements also. Hope this helps.
I had the following problems with the existing solutions, and found something that seems to solve all of them. This assumes you're aiming for something cross browser, cross device, and don't want device sniffing.
The problems this solves
Using just
touchstart
ortouchend
:Triggering
mouseover
events ontouchstart
andmouseout
ontouchmove
has less serious consequences, but does interfere with the usual browser behaviour, for example:touchstart
like amouseover
, which ismouseout
ed on the nexttouchstart
. One way to see mouseover content in Android is therefore to touch the area of interest and wiggle your finger, scrolling the page slightly. Treatingtouchmove
asmouseout
breaks this.The solution
In theory, you could just add a flag with
touchmove
, but iPhones trigger touchmove even if there's no movement. In theory, you could just compare thetouchstart
andtouchend
eventpageX
andpageY
but on iPhones, there's notouchend
pageX
orpageY
.So unfortunately to cover all bases it does end up a little more complicated.
In theory, there are ways to get the exact time used for a longpress instead of just using 1000 as an approximation, but in practice it's not that simple and it's best to use a reasonable proxy.
You could check
navigator.userAgent
like this:but you would have to check for blackberries, droids, umpty other touchscreen devices. You could also bind the mouseovers only if the userAgent contains Mozilla, IE, Webkit, or Opera, but you still need to screen for some devices because the Droid, for instance, reports its userAgent string as:
The iPhone's string is similar. If you just screen for iPhone, iPod, iPad, Android and Blackberry you might get the majority of handhelds, but not all of them.
To get the links working without breaking touch scrolling, I solved this with jQuery Mobile's "tap" event:
Haven't tested this fully but since iOS fires touch events, this could work, assuming you are in a jQuery setting.
The idea is that Mobile WebKit fires a
touchend
event at the end of a tap so we listen for that and then redirect the browser as soon as atouchend
event has been fired on a link.MacFreak's answer was extremely helpful to me. Here's some hands-on code in case it helps you.
PROBLEM - applying touchend means every time you scroll your finger over an element, it responds as if you've pressed it, even if you were just trying to scroll past.
I'm creating an effect with jQuery which fades up a line under some buttons to "highlight" the hovered button. I do not want this to mean you have to press the button twice on touch devices to follow the link.
Here are the buttons:
I want the "menu_underline" div to fade up on mouseover and fade out on mouseout. BUT I want touch devices to be able to follow the link on one single click, not two.
SOLUTION - Here's the jQuery to make it work:
Many thanks for your help on this MacFreak.