2013-08-02 60 views
0

我有这个简单的小提琴作为工作示例 -如何检测鼠标是否为真?

Jsfiddle

我试图从div部分检测mouseout事件。 当我鼠标悬停在这张图片上时,它显示标题;说“改变形象”。 5秒后,字幕变淡。

我正在使用setInterval来相应地设置它。现在,如果我做了这个图像的鼠标移出,那么只需要调用Interval函数。

我如何检测jQuery中的mouseout事件?

Tried-

$(function() { 
     $('.image-profile').mouseover(function() { 
      $('.change-image').stop().show(); 

      if ($('.image-profile').mouseout()== true) { 
       TimeOut(); 
      } 
     }); 

     setInterval(function TimeOut() { 
      $('.change-image').fadeOut() 
     }, 5000 

     ); 
    }); 
+3

'mouseover',为什么会'mouseout'工作有什么不同? – Yoshi

+2

+我敢打赌你不想为此使用'setInterval()'。改用'setTimeout()'。 – Teemu

+0

@Yoshi,只是因为我想检测mouseout事件,使用它是另一种做法。 – Manoj

回答

0
$('.image-profile').on('mouseleave', function() { 
    setTimeout(function() { 
     $('.change-image').fadeOut() 
    }, 5000); 
}); 
1
var ImageProfileTimer; 

$('.image-profile').on('mouseenter',function(){ 
    clearTimeout(ImageProfileTimer); 
    $('.change-image').stop().show(); 
}).on('mouseleave',function(){ 
    ImageProfileTimer = setTimeout(function(){ 
     $('.change-image').fadeOut() 
    }, 5000); 
}); 

使用setTimeout和clearTimeout

演示:http://jsfiddle.net/xMNTB/9/

+0

可以在不使用mouseleave的另一个功能的情况下完成吗?我想检测mouseout是否为真。 – Manoj

+0

是的,你可以这样做@Manoz只是改变mouseleave鼠标 – l2aelba

+0

+ http://stackoverflow.com/questions/4258615/what-is-the-difference-between-jquerys-mouseout-and-mouseleave – l2aelba

0

http://jsfiddle.net/xMNTB/7/

现在div显示在鼠标上,并在鼠标离开5秒后消失。

$(function() { 

    $('.image-profile').mouseenter(function() { 
     $('.change-image').stop().show(); 
    }); 

    $('.image-profile').mouseleave(function() { 
     setTimeout(function TimeOut() { 
      $('.change-image').fadeOut() 
     }, 5000); 
    }); 

}); 
0

试试这个:

(function() { 
    $('.image-profile').mouseover(function() { 
     $('.change-image').stop().show(); 

     if ($('.image-profile').mouseout() == true) { 
      TimeOut(); 
     } 
    }).mouseout(function() { 
     setInterval(function TimeOut() { 
      $('.change-image').fadeOut() 
     }, 5000); 
    }); 
}); 

JSFIDDLE DEMO

您已经使用