4

I have the following HTML

<div class="individual">
    <div class="change">change</div>
    <div class="picture"><img src....></div>
</div>

The .change has position: absolute; and is invisible. On mouseover .picture, I want .change to appear, and on mouseout, to disappear. If the individual clicks .change then something should happen.

Right now, when mouse goes over change, then it is treated as mouseout of picture, and so change starts to flicker!

I then made this jQuery:

$('.change').mouseout(function(){
    $('.picture').mouseout(function(){
        $(this).parent().children('.change').hide();
    });
});

$('.picture').mouseover(function(){
    var i = $(this).parent().children('.change').show();
});

This works fine only the first time! If I move out the picture, then when I come back on, and go on change, everything starts to flicker!! What do I do?!

Thanks

devnull69
  • 16,402
  • 8
  • 50
  • 61
user1083320
  • 1,836
  • 8
  • 22
  • 29

4 Answers4

17

Use 'mouseenter' instead of 'mouseover' and 'mouseleave' instead of 'mouseout'

$('.picture').mouseenter(function(){
   $(this).parent().children('.change').hide();
});



$('.picture').mouseleave(function(){
    var i = $(this).parent().children('.change').show();
});
luckystars
  • 1,704
  • 12
  • 12
  • 2
    "they travel up the DOM with each mouseover / mouseout event triggering to see if the user has truly "entered" or "left" the given element." – Johnston Jun 13 '13 at 18:23
  • Was looking for a React solution... The DOM hasn't changed. +1 :) – Laoujin Sep 14 '19 at 00:44
3

I have always found mouseout and mouseover to be rather buggy. Not sure why, you might try:

$(function() { 
    $('.picture').hover(function() {
        $('.change').show();
    },function() { $('.change').hide(); });
});

As long as the appearance of ".change" doesn't move ".picture", causing the hover to be broken, then this should work for you.

Ravi Y
  • 4,296
  • 2
  • 27
  • 38
2

Try:

.change { 
    pointer-events: none;
}
Onshop
  • 2,915
  • 1
  • 27
  • 17
1

The problem is that hiding .change will move the .picture element to the place where .change used to be. This will trigger both .mouseover() and .mouseenter() so there's no point to replace.

You should change CSS positioning to absolute for both elements so removing .change won't move .picture

devnull69
  • 16,402
  • 8
  • 50
  • 61