简体   繁体   中英

How do I avoid hover event to fire in iPhone and iPad?

I had added an effect on hoverEnter and hoverLeave on my Divs assuming that these events wont fire on iOS devices. But on iOS devices the hoverEnter and click event fire and the hoverEnter effect is maintained. I don't want these hover events to fire on mobile devices. What should I do. I am attaching event via Javascript and the hover event is attached to the div before click event.

To emulate the hover you simply add an event listener to the element you want to have a hover event, you can use touchstart and touchend events instead of using hover

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
    $(".menu li a").bind('touchstart', function(){
        console.log("touch started");
    });
    $(".menu li a").bind('touchend', function(){
        console.log("touch ended");
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM