jQuery 检测鼠标在元素上停留的秒数

发布于 2024-12-01 08:06:55 字数 739 浏览 0 评论 0原文

有没有办法检测鼠标指针在 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 }

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 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手长情犹 2024-12-08 08:06:56

给定这个标记:

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

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
});
时间你老了 2024-12-08 08:06:56

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

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

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);
    }
})
});
二货你真萌 2024-12-08 08:06:56

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

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

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
});
生来就爱笑 2024-12-08 08:06:56

我使用了 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")}
    }
})

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")}
    }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文