Can hover and click functions be combined into one, so for example:
click:
$('#target').click(function() {
// common operation
});
hover:
$('#target').hover(function () {
// common operation
});
can they be combined into one function?
Thanks!
You can click or drag simply by hovering your mouse pointer over a control or object on the screen. This is useful if you find it difficult to move the mouse and click at the same time. This feature is called Hover Click or Dwell Click.
The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element.
fn. hover() is deprecated #66.
Definition and Usage The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.
Use basic programming composition: create a method and pass the same function to click
and hover
as a callback.
var hoverOrClick = function () {
// do something common
}
$('#target').click(hoverOrClick).hover(hoverOrClick);
Second way: use bind
on
:
$('#target').on('click mouseover', function () {
// Do something for both
});
jQuery('#target').bind('click mouseover', function () {
// Do something for both
});
Use mouseover instead hover.
$('#target').on('click mouseover', function () {
// Do something for both
});
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