Reputation: 55
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?
Upvotes: 5
Views: 34828
Reputation: 1326
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
Upvotes: 3
Reputation: 253358
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();
});
Upvotes: 6
Reputation: 10057
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();
});
Upvotes: 3