Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hover callback function with on() and binding?

Hi im adding some elements dynamically, now im trying to bind and a hover() using on() but that does not seem to work with a callback function. any ideas?

jQuery:

$(document.body).on('hover', 'div.settings-container', function () {
     $(this).find('ul.settings-links').fadeIn();
}, function () {
     $(this).find('ul.settings-links').fadeOut();
});

the jsfiddle is simplified.

like image 474
Dejan.S Avatar asked Oct 12 '12 06:10

Dejan.S


People also ask

What is the difference between Mouseout and Mouseleave?

This means that mouseleave is fired when the pointer has exited the element and all of its descendants, whereas mouseout is fired when the pointer leaves the element or leaves one of the element's descendants (even if the pointer is still within the element).

What is the difference between mouseover and Mouseenter?

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.

How can write hover function in jQuery?

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. Syntax: $(selector). hover(Function_in, Function_out);


2 Answers

In jQuery, $(selector).hover(handlerIn, handlerOut) is just a shortcut for

$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

hover is not an event, you need to use mouseenter and mouseleave instead.

$('body').on({
  mouseenter: function() {
     $(this).find('ul.settings-links').fadeIn();
  },
  mouseleave: function() {
     $(this).find('ul.settings-links').fadeOut();
  }
}, 'div.settings-container');
like image 146
xdazz Avatar answered Sep 30 '22 18:09

xdazz


Method "on" uses "hover" as shortcut for both events - mouseenter and mouseleave use event.type to detect them

$(document).on('hover', 'a.hover-me', function (e) {
    if (e.type == "mouseenter") {
        alert("show");
    } else {
        alert("hide");
    }
});​

Fiddle

like image 26
Denis Ermolin Avatar answered Sep 30 '22 16:09

Denis Ermolin