Reputation: 567
I've used this jquery for a couple applications which basically does an img swap with a fading transition. Super simple
$(document).ready(function() {
$("img.a").hover(
function() {
$(this).stop().animate({
"opacity": "0"
}, "fast");
}, function() {
$(this).stop().animate({
"opacity": "1"
}, "fast");
});
});
with an HTML mark up this
<div id="leftBox" class="fadehover inline">
<img src="images/leftTop.jpg" alt="" class="a" />
<img src="images/leftBot.jpg" alt="" class="b" />
</div>
CSS
/*fadeHover*/
div.fadehover {
position: relative;
}
img.a {
position: absolute;
left: 0;
top: 0;
z-index: 10;
}
img.b {
position: absolute;
left: 0;
top: 0;
}
I'm wondering if this can be applied to a background image?
I'm trying to do it with in the style tag but not having any luck.
Thanks
Upvotes: 1
Views: 268
Reputation: 567
I ended up doing this
$(document).ready(function(){
$('#home').mouseenter(function(){
$('.home_hover').fadeIn();
});
$('#home').mouseleave(function(){
$('.home_hover').fadeOut();
});
});
<div id="home">
<div class="home_normal">Home</div>
<div class="home_hover">Home</div>
</div>
with CSS
#home{
position:relative;
float: left;
height: 25px;
width: 88px;
padding-top: 12px;
}
.home_normal{
position:absolute;
height: 25px;
width: 88px;
left: 0px;
top: 0px;
padding-top: 12px;
}
.home_hover{
background-image:url(../images/menuBG-1.0.jpg);
height: 25px;
width: 88px;
position:absolute;
display: none;
left: 0px;
top: 0px;
padding-top: 12px;
}
Upvotes: 0
Reputation: 2034
I wouldn't necessarily do it the way you are, but it can be achieved by simultaneously animating the other image. Based on your html, this should do it:
$("img.a").hover(
function() {
$(this).stop().animate({"opacity": "0"}, "fast");
$("img.b").animate({"opacity": "1"}, "fast");
},
function() {
$(this).stop().animate({"opacity": "1"}, "fast");
$("img.b").animate({"opacity": "0"}, "fast");
}
);
Upvotes: 1
Reputation: 10520
Yes, It can be possible. You JS code would work fine for that scenario as well. Just need to modify the img.a css class a bit.
img.a {
display: block;
background: url('your-image') no-repeat bottom left;
padding: 0 0px 32px 200px;
}
Please note that the padding attribute plays a vital role here and before setting it correctly you should know the height and width of the background image, otherwise image will not be visible fully.
Demo: http://jsfiddle.net/V7wB6/
Upvotes: 0