Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate hover effect on touch devices?

I have a web page with some css3 animation on hover and I would like to make it work on iPad (and any touch enabled device) when the user touch these elements.
Here's my code:

html:

<div id="headBlock">
    <a id="arm"></a>
    <div id="head">
        <div id="blink"></div>
        <div id="lid"></div>
        <div id="man"></div>
        <a id="man-arm"></a>
    </div>
</div>

js:

//arm
$('#arm').hover(function(){
    $(this).removeAttr('style').css('bottom','244px');
    $(this).addClass('hover_effect');

    }, function(){
        $(this).removeClass('hover_effect');
});

//man arm
$('#man').hover(function(){
    $('#man-arm').removeAttr('style');
    $('#man-arm').addClass('hover_effect');

    }, function(){
        $('#man-arm').removeClass('hover_effect');
});

You can see that when the mouse enter the elements I remove the style then apply a style and add the css class 'hover_effect' and when the mouse leaves I remove the 'hover_effect' class.
How can I achieve the same thing on touch devices? The click event doesn't have the a callback so I cannot remove the 'hover_effect' class after the click happens. I tried mousedown and mouseup but it didn't work. I searched stackoverflow and I found this How do I simulate a hover with a touch in touch enabled browsers? but it didn't have any effect.
Any idea anyone?

Thanks
Mauro

like image 572
Mauro74 Avatar asked Jan 23 '12 10:01

Mauro74


People also ask

How do I simulate a hover with a touch in touch enabled browsers?

To simulate a hover with a touch in touch enabled browsers with JavaScript, we add a touchstart event listener and some styles. document. addEventListener("touchstart", () => {}, true); to add a touchstart event listener to document .

Does mouseover work on mobile?

There are no mouse devices on mobile, so users don't have the luxury of using hover effects. Using a hover effect on mobile apps causes buttons to stay stuck in the hovered state when tapped. This stickiness not only confuses users, but it frustrates them when they're forced to double-tap buttons to initiate an action.


1 Answers

try this

$('#arm').bind('touchstart', function() {
        $(this).removeAttr('style').css('bottom','244px');
        $(this).addClass('hover_effect');
});

$('#arm').bind('touchend', function() {
        $(this).removeClass('hover_effect');
});

similarly for $('#man').

like image 128
dku.rajkumar Avatar answered Oct 05 '22 06:10

dku.rajkumar