5

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?

Paul Smith
  • 55
  • 1
  • 1
  • 4

3 Answers3

6

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.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • I know its too many days ago but i came across same requirement. I think we must set display to inline-block for that wrap class (moving mouse pointer right side of that image Still shows the overlay) except that its perfect Answer – Vivekh Feb 22 '14 at 17:42
3

Instead of checking the .company-image element, you're going to want to check the overlay. Try the following.

$('.company-image').on("mouseover", function () {
    $('.company-image-overlay').show();
});

$('.company-image-overlay').on("mouseout", function () {
    $('.company-image-overlay').hide();
});
jeremy
  • 9,965
  • 4
  • 39
  • 59
3

If i were you i would use only css. Actually you do not need any kind of functions like show() or hide(). I used an tag for wrapping because some old Internet Explorer versions does know about :hover only on this tag.

You can check the trick here

stefanz
  • 1,316
  • 13
  • 23
  • Ahh I see stefanz, so you're actually making the image opaque to let the background 'shine through' as it were. This is the kind of idea I had at first, but resorted to JQuery. Excellent. I would accept this as the correct answer but since I did ask about JQuery, I'll go with a JQuery example, but I originally (before I posted this question) was trying for an all CSS option. – Paul Smith Mar 31 '13 at 08:02
  • +1 Great suggestion, with minimal JavaScript, and works better than the other solutions. – Matt Nov 10 '14 at 22:09