jQuery detect how many seconds a mouse stays over element

橙三吉。 提交于 2019-12-07 17:35:43

问题


is there any way to detect how many seconds a mouse pointer stays on an html element?

I would like to retrieve how many seconds a mouse stays over element to put a little delay on a callback event... if is possible :)

i'm trying with a simple for() cycle detecting by a counter :

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

the problem is that it doesn't works :(

also i would like to bind a mouseleave event, script logic should be:

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 }

回答1:


You should be able to utilize the hover() function to capture when the mouse goes over a particular element and then react as desired when the mouse is removed from that object.

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



回答2:


Given this markup:

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

Something like this plugin should do the trick:

(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);

and the calling code:

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

Live example: http://jsfiddle.net/nrUqS/

For your requirement, something like this should suffice:

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



回答3:


This code will calculate the time in milliseconds that you hover over an element with your mouse:

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



回答4:


I've used C. Spencer Beggs answer as a template, because his one didn't work for me. I've used simple variables, included lots of console.log messages and corrected '==' code to '='. This example will wait 3 seconds of 'hover over a link' action to take place before acting. HTH someone.

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


来源:https://stackoverflow.com/questions/7150219/jquery-detect-how-many-seconds-a-mouse-stays-over-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!