简体   繁体   English

如何避免在 iPhone 和 iPad 中触发悬停事件?

[英]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.假设这些事件不会在 iOS 设备上触发,我在我的 Div 上添加了对 hoverEnter 和 hoverLeave 的效果。 But on iOS devices the hoverEnter and click event fire and the hoverEnter effect is maintained.但是在 iOS 设备上,hoverEnter 和 click 事件会触发,而 hoverEnter 效果会保持不变。 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.我正在通过 Javascript 附加事件,并且在单击事件之前将悬停事件附加到 div。

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要模拟悬停,您只需向要具有悬停事件的元素添加一个事件侦听器,您可以使用 touchstart 和 touchend 事件而不是使用悬停

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");
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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