Is there a JavaScript or jQuery solution to run a function repeatedly (after setTimeout
) while the mouse is over a DOM object? Otherwise said, is there a JavaScript "do while mouseover" (or "if mouseover")?
$('someObject').bind('mouseover', function() {
//Do the following while mouseover
$('someOtherObject').css('margin-left',adjustedLeft + 'px');
setTimeout(/*do it again*/,25);
});
The hover()method binds handlers for both mouseenter and mouseleave events. Basically, with the hover() method, we will specify what to do when the cursor enters the element and we will specify what to do when the cursor leaves that element. Parameters: It accepts two functions i.e. handlerIn and handlerOut.
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. The onmousemove event triggers every time the mouse pointer is moved over the div element.
The mouseover() method is an inbuilt method in jQuery which works when mouse pointer moves over the selected elements. Parameters: This method accepts single parameter function which is optional. This parameter is used to specify the function to run when the mouseover event is called.
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.
$('someObject').on('mouseenter', function() {
this.iid = setInterval(function() {
// do something
}, 25);
}).on('mouseleave', function(){
this.iid && clearInterval(this.iid);
});
Example Look here
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