【问题标题】:jQuery mouseleave for touch screen / tablets用于触摸屏/平板电脑的 jQuery mouseleave
【发布时间】:2014-01-19 21:00:45
【问题描述】:

我有一个模态框,它在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");

}); 

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      $('.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");        
      });
      

      【讨论】:

      • 谢谢,但我试图在用户触摸页面的任何时候获取,如果模式可见则隐藏它。
      • 你想关闭任何可以打开的tiptip架吗?
      • 是的,如果有任何tiptop_holder 可见,我想在触摸身体时将它们全部隐藏。我一直在尝试它,但它隐藏一次后,我无法让它重新出现。
      • 非常感谢! body touchstart 的问题是 .tiptrigger 位于 body 内,所以现在它试图同时打开和关闭。有没有办法,而不是$('body').on('touchstart') 基本上说明除了.tiptrigger 之外是否有任何东西被触摸?应该这样做。
      • 我需要你知道它是如何工作的,它是打开一个覆盖 div 吗?你能提供一个关于 jsfiddle 的快速示例吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多