Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery mouseover image overlay

Just wondering how I can get this working 100% correctly. I think I'm nearly there.

Basically, I have an image & when I mouseover, I want an overlay (which is a coloured div) to appear over the top.

I have this semi-working in this fiddle.

<img src="http://mirrorchecker.com/images/rod_160.png" width="160"
class="company-image"/>
<div class="company-image-overlay"></div>

/* CSS */
.company-image
{
}
.company-image-overlay
{
width: 160px;
height: 160px;
background-color: #ffb00f;
z-index: 1;
opacity: 0.5;
    position: fixed;
    top: 0.5em;
    display:none;
}

/* JQUERY */
$('.company-image').mouseover(function()
     {
        $('.company-image-overlay').show();
     });
$('.company-image').mouseout(function()
     {
       $('.company-image-overlay').hide();
     });

The issue seems to be when the overlay appears, the mouse is no longer technically over the .company-image therefore we get a constant cycling of over / out and the flashing background.

Any ideas?

like image 361
Paul Smith Avatar asked Mar 30 '13 20:03

Paul Smith


1 Answers

The simplest solution is to add a wrapping element for both elements:

<div class="wrap">
    <img src="http://mirrorchecker.com/images/rod_160.png" width="160" class="company-image" />
    <div class="company-image-overlay"></div>
</div>

And place the mouseover/mouseout methods to that element instead:

$('.wrap').mouseover(function () {
    $('.company-image-overlay').show();
}).mouseout(function () {
    $('.company-image-overlay').hide();
});

JS Fiddle demo.

like image 129
David Thomas Avatar answered Oct 05 '22 08:10

David Thomas