【问题标题】:jQuery detect how many seconds a mouse stays over elementjQuery检测鼠标停留在元素上的秒数
【发布时间】:2011-08-22 16:01:52
【问题描述】:

有没有办法检测鼠标指针停留在 html 元素上的秒数?

我想检索鼠标停留在元素上的秒数以对回调事件施加一点延迟...如果可能的话:)

我正在尝试通过计数器检测一个简单的 for() 循环:

var time_over ; 
$('.bean-active').live('mouseover',function(){  
  id_tag = $(this).attr("id");   
  for(time_over = 1;time_over <= 3000;time_over ++){
      if(time_over == 3000){
         $('.bean-bubble,.bean-bubble img').hide();   
         $('#bean-bubble-'+id_tag+',#bean-bubble-'+id_tag+' img').show();
      }  
  }   
});

问题是它不起作用:(

我也想绑定一个mouseleave事件,脚本逻辑应该是:

while ( mouseover element count how many time it stays over) 
  if (time == n)
  { do somenthing } 
if (mouseleave from element earlier then time)
{ do somenthing different }

【问题讨论】:

  • 现在使用它:) 但似乎超时对我不起作用:P
  • $('.bean-active').live('mouseover',function(){ $(this).hoverIntent({ over: function(){ id_tag = $(this).attr ("id"); $(this).fadeTo(100,0.5).fadeTo(200,1); $('.bean-bubble,.bean-bubble img').hide(); $('#bean -bubble-'+id_tag+',#bean-bubble-'+id_tag+'img').show(); }, timeout:900, out: function(){ return false; } }); $(this).trigger ('鼠标悬停'); });
  • 在没有鼠标离开的情况下可以计算秒数吗?因为我需要在三秒后加载数据而不是预加载数据。

标签: javascript jquery jquery-plugins mouseevent mouseover


【解决方案1】:

鉴于此标记:

<div id="hoverOverMe">Hover over me</div>
<div id="output"></div>

类似这个插件的东西应该可以解决问题:

(function($) {
    $.fn.delayedAction = function(options)
    {
        var settings = $.extend(
            {},
            {
                delayedAction : function(){},
                cancelledAction: function(){},
                hoverTime: 1000               
            },
            options);

        return this.each(function(){
           var $this = $(this);
            $this.hover(function(){  
               $this.data('timerId',
                          setTimeout(function(){
                                      $this.data('hover',false);
                                      settings.delayedAction($this);
                                      },settings.hoverTime));
                $this.data('hover',true);
            },
            function(){        
                if($this.data('hover')){       
                    clearTimeout($this.data('timerId'));
                    settings.cancelledAction($this);
                }
                $this.data('hover',false);
            } );
        });
    }
})(jQuery);

和调用代码:

$('#hoverOverMe').delayedAction (
    {
        delayedAction: function($element){
            $('#output').html($element.attr('id') + ' was hovered for 3 seconds');
        },
        cancelledAction: function($element){
            $('#output').html($element.attr('id') + ' was hovered for less than 3 seconds');
        },
        hoverTime: 3000 // 3 seconds
    }
);

现场示例:http://jsfiddle.net/nrUqS/

对于您的要求,这样的东西就足够了:

$('.bean-active').delayedAction(
{ 
   delayedAction: function($element){  
       id_tag = $element.attr("id");   
       $('.bean-bubble,.bean-bubble img').hide();   
       $('#bean-bubble-'+id_tag+',#bean-bubble-'+id_tag+' img').show();
    },
    hoverTime: 3000
});

【讨论】:

    【解决方案2】:

    此代码将计算您将鼠标悬停在元素上的时间(以毫秒为单位):

    $(document).ready(function() {
    $('#element').bind('mouseenter mouseleave', function(evt) {
        var currentTime == new Date();
        if (evt.type === 'mouseenter') {
            $(this).data('mouseenterTime') == currentTime.getTime();
        } else if (evt.type === 'mouseleave') {
            var mouseoverTime = currentTime.getTime() - $(this).data('mouseenterTime');
            alert('mouseover time was: ' + mouseoverTime);
        }
    })
    });
    

    【讨论】:

    • 我喜欢你设置绑定的方式。它使逻辑易于组织和重用,但是当我运行此功能时,随着时间的推移,我得到了鼠标的 NAN。你得到这个工作了吗?
    • 在没有鼠标离开的情况下可以计算秒数吗?因为我需要在三秒后加载数据而不是预加载数据。
    【解决方案3】:

    您应该能够利用hover() 函数在鼠标移过特定元素时进行捕捉,然后在鼠标从该对象上移开时根据需要做出反应。

    $("#someDiv").hover(function(){ 
        //start counter
    }, function(){
        //stop counter
    });
    

    【讨论】:

      【解决方案4】:

      我使用 C. Spencer Beggs 的答案作为模板,因为他的答案不适合我。我使用了简单的变量,包括大量的 console.log 消息并将 '==' 代码更正为 '='。此示例将等待 3 秒的“将鼠标悬停在链接上”操作,然后再执行操作。 HTH 某人。

      var mouseenterTime = 0;
      
      $(document).on('mouseenter mouseleave', '#element', function (evt)
      {
          var currentTime = new Date();
          if (evt.type === 'mouseenter') 
          {
              mouseenterTime = currentTime.getTime();
              console.log('mouseenterTime (#1): ' + mouseenterTime);
          } else if (evt.type === 'mouseleave') {
              console.log('mouseenterTime (#2): ' + mouseenterTime);
              var mouseoverTime = currentTime.getTime() - mouseenterTime;
              console.log('mouseover time was: ' + mouseoverTime);
      
              // Checking if the Hover action has latest for longer than 3 seconds. 
              if(mouseoverTime > 3000) {console.log("Three seconds have elapsed")}
          }
      })
      

      【讨论】:

      • 优秀。 mouseenterTime 需要在函数之外。
      • 在没有鼠标离开的情况下可以计算秒数吗?因为我需要在三秒后加载数据而不是预加载数据。
      猜你喜欢
      • 2010-12-12
      • 2011-06-02
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      相关资源
      最近更新 更多