I would like to add and remove classes on elements when a user hovers over an element, but only if their cursor has been on it for more than 1 second for example. How can I achieve this?
$("#thumbs div").mouseenter(function() {
$('#thumbs div').removeClass('hovered');
$(this).addClass('hovered');
});
The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element.
In computing, a mouseover , mouse hover or hover box is a graphical control element that is activated when the user moves or hovers the pointer over a trigger area, usually with a mouse, but also possible with a digital pen. Mouseover control elements are common in web browsers.
The mouseover event is fired at an Element when a pointing device (such as a mouse or trackpad) is used to move the cursor onto the element or one of its child elements.
Use a timer:
var timer;
$("#thumbs div").mouseenter(function() {
var that = this;
timer = setTimeout(function(){
$('#thumbs div').removeClass('hovered');
$(that).addClass('hovered');
}, 1000);
}).mouseleave(function() {
clearTimeout(timer);
});
http://jsfiddle.net/qGRcH/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With