jQuery mouseleave用于触摸屏/平板电脑

时间:2014-01-02 12:02:09

标签: javascript jquery html css

我有一个模式框,在mouseenter上淡入并在mouseleave上淡出。唯一的问题是当使用平板电脑等触摸屏设备时,我无法在页面上显示模式fadeout。是否有任何方法可以将此代码修改为用户触摸模态框外的任何时间fadeout

$(".tiptrigger").mouseenter(function() { 

    var s_id = $(this).attr('id'); // There may be more than one per page
    $("#tiptip_holder-"+s_id).fadeIn("fast");   

}); 

$(".tiptrigger").mouseleave(function() { 

    var s_id = $(this).attr('id');
    $("#tiptip_holder-"+s_id).fadeOut("slow");

}); 

2 个答案:

答案 0 :(得分:16)

您可以尝试使用触摸事件(未测试):

$('.tiptrigger').on('mouseenter touchstart', function(){ 
    var s_id = $(this).attr('id'); // There may be more than one per page
    $("#tiptip_holder-"+s_id).fadeIn("fast"); 
});

$('.tiptrigger').on('mouseleave touchend', function(){
    var s_id = $(this).attr('id');
    $("#tiptip_holder-"+s_id).fadeOut("slow");
});

修改

您可以尝试:

$('.tiptrigger').on('mouseenter touchstart', function(e){ 
    var s_id = $(this).attr('id'); // There may be more than one per page
    $("#tiptip_holder-"+s_id).fadeIn("fast"); 
    e.stopPropagation()
});

$('.tiptrigger').on('mouseleave', function(e){
    var s_id = $(this).attr('id');
    $("#tiptip_holder-"+s_id).fadeOut("slow");
});

$('body').on('touchstart', function(e){ 
    $("div[id^='tiptip_holder']").fadeOut("slow");        
});

答案 1 :(得分:7)

mouseMouse事件(mouseovermouseoutmousedownmouseupmousemove等特定于鼠标输入设备。键盘有keydownkeypresskeyup。触摸有touchstarttouchmovetouchendtouchcancel。 iPhone / iPad /等上的Webkit具有Apple特定的附加手势开始/移动/结束事件。

因此,您应该检查运行应用程序的设备,然后相应地处理代码。

相关问题