Reputation: 8531
When you hover over my image, a button will appear. The problem is that when the user tries to click on the button it starts to flicker the user is now off the <img>
tag. I cannot place the jQuery hover()
selector to detect when the user is hovering over my entire item as I have text under the image and I do not want the button to appear when the user is hovering over the text. How would I get the <button>
to not flicker when the user tries to click it.
<div class="item">
<img src="http://www.placehold.it/250x100">
<button>More Info</button>
<p>Description</p>
</div>
button { position:absolute; top:20px; left:90px; display:none; }
$('.item img').hover( function() {
$(this).parent().find('button').show();
}, function() {
$(this).parent().find('button').hide();
}
);
Upvotes: 1
Views: 2116
Reputation: 19729
It flickers because when the mouse enters, or hovers over the button, it leaves the image. To fix it, move the event handler to the <div>
:
$('.item div').hover( function() {
$(this).find('button').show();
}, function() {
$(this).find('button').hide();
}
);
Edit: Place a <div>
inside of your .item
with the image and the button wrapped inside of it, as such:
<div class="item">
<div>
<img src="http://www.placehold.it/250x100">
<button>More Info</button>
</div>
</div>
Upvotes: 2
Reputation: 2789
Apply the hover effect to the image and the button:
$('.item img, .item button').hover( function() {
$(this).parent().find('button').show();
}, function() {
$(this).parent().find('button').hide();
}
);
Upvotes: 2