0

So I want this div to fade another div that has a black background at 0.5 opacity over it. But when i hover it ignores the image already in the div and fills a black then fades the other div. so it goes Image --> Solid black div --> then fades my second div. https://jsfiddle.net/70e890e6/

HTML:

<div class="box1">
    <div class="img_hover_effect"></div>
    <img src="images/portfolio/charity.png" class="box1_img">
    <img src="images/portfolio/charity/2.png" class="box1_img2">
</div>

CSS:

.img.img_hover_effect {

display: none;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);

}

JQuery:

$(document).ready(function(){

   $('.box1').on('mouseenter', function() {

$('.img_hover_effect').fadeIn(1000);

});

});
2

2 Answers 2

1

You can use jQuery hover like this :

$('.box1').hover(function() {
    $('.hover').fadeIn(1000);
}, function(){
    $('.hover').fadeOut(1000);
});

and some css changes :

.box1 {
    position:relative;
    height: 400px;
    width: 350px;
    margin: 0 auto;
    margin-top: 100px;
    float: left;
    overflow: hidden;
}

.tree {
    height: 100%;
    width: auto;
    display: block;
    position: relative;
}

.hover {
    position:absolute;
    background-color: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    display: none;
    z-index: 10;
}

JSFIDDLE : https://jsfiddle.net/70e890e6/4/

0
1

You could use a CSS pseudo-class for the desired hover effect. I'd recommend applying the class directly to the images you wish to have the hover effect rather than overlaying with another div.

.hover-effect:hover{
    background-color: #000000; //or whatever colour fade you are looking for
    opacity: 0.5;
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.