Can not get jquery hover to work with .live ()

second function doesn't work?

$('.edit_hover').live('hover',
    function(e){        
        $(this).stop();
        var half_width = ($(this).css('width').slice(0, -2))/2;
        var half_height = ($(this).css('height').slice(0, -2))*0.3;
        console.log(half_width);
        var top = ($(this).position().top) + half_height;
        var left = ($(this).position().left) + half_width;
        $('#edit_hover').css('top', top).css('left', left).fadeIn(300);
        //add overlay
        $(this).css('position', 'relative').append('<div class="edit_overlay" style="position: absolute; top:0px; left:0px; height:100%; width: 100%; background: #999; opacity: 0.5;"></div> ')
    },
    function(){
        $(this).stop();
        $(this).find('.edit_overlay').remove();
        $('#edit_hover').fadeOut(300);
    }); 

      

+2


a source to share


3 answers


live()

only accepts one handler, so you can't use hover

(before 1.4.1). Anyway, this is just a shortcut for mouseenter

and mouseleave

. Use these events to bind to:

$('.edit_hover')
.live('mouseenter',function(){})
.live('mouseleave',function(){}); 

      



Or, since jQuery 1.4.1:

$('.edit_hover').live('hover', function(event) {
  if (event.type == 'mouseenter') {
    // first function here
  } else {
    // second function here
  }
});

      

+11


a source


Just guess: don't use hover

use mouseover

and mouseout

:



$("...").live("mouseover", function() {
    // ....
}).live("mouseout", function() {
    // ...
});

      

0


a source


Which version of jQuery? You must be using jQuery 1.4.1 for hover

live use . It was not supported in earlier versions. See the Caveats section of the live

documentation
. Note, however, that you only have one callback and need to distinguish the event within the callback to handle the hover / out independently.

$('.hoverme').live('hover', function(event) {
  if (event.type == 'mouseover') {
    // do something on mouseover
  } else {
    // do something on mouseout
  }
});

      

0


a source







All Articles