我有一个网页,在悬停时有一些 css3 动画,我希望当用户触摸这些元素时它可以在 iPad(以及任何支持触摸的设备)上运行。
这是我的代码:
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');
});
您可以看到,当鼠标进入元素时,我删除样式,然后应用样式并添加 css 类“hover_effect”,当鼠标离开时,我删除“hover_effect”类。
如何在触摸设备上实现同样的效果?单击事件没有回调,因此我无法在单击发生后删除“hover_effect”类。我尝试了 mousedown 和 mouseup 但没有成功。我搜索了 stackoverflow,发现了这个 How do I simulate a hover with a touch in touch enabled browsers?但没有任何效果。
有人知道吗?
谢谢
毛罗
最佳答案
试试这个
$('#arm').bind('touchstart', function() {
$(this).removeAttr('style').css('bottom','244px');
$(this).addClass('hover_effect');
});
$('#arm').bind('touchend', function() {
$(this).removeClass('hover_effect');
});
与$('#man')类似。
关于jquery - 如何在触摸设备上模拟悬停效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8970280/