Andrés Alagna Andrés Alagna - 6 months ago 13
jQuery Question

How do i make my hover caption to cover the whole image size?

I'm trying to make a hover caption that adapts itself to the full image width and height even when it resizes. I've tried using a jquery code that I found here but I haven't been able to figure out how to make it work.



$(document).ready(function() {
var imgWidth = $('.imagen').width();
$('.caption-text').css({width: imgWidth});
$('.caption-style-1').css({width: imgWidth});
$('.blur').css({width: imgWidth});
});

.container-a1 {
height: 100%;
width: 100%;
}
.caption-style-1{
list-style-type: none;
margin: 0px;
padding: 0px;

}

.caption-style-1 li{
float: left;
padding: 0px;
position: relative;
overflow: hidden;
}

.caption-style-1 li:hover .caption{
opacity: 1;

}


.caption-style-1 img{
margin: 0px;
padding: 0px;
float: left;
z-index: 4;
}


.caption-style-1 .caption{
cursor: pointer;
position: absolute;
opacity: 0;
-webkit-transition:all 0.45s ease-in-out;
-moz-transition:all 0.45s ease-in-out;
-o-transition:all 0.45s ease-in-out;
-ms-transition:all 0.45s ease-in-out;
transition:all 0.45s ease-in-out;

}
.caption-style-1 .blur{
background-color: rgba(0,0,0,0.65);
position: absolute;
height: 500px;
width: 400px;
z-index: 5;
position: relative;
}

.caption-style-1 .caption-text h1{
text-transform: uppercase;
font-size: 24px;
}
.caption-style-1 .caption-text{
z-index: 10;
color: #fff;
position: absolute;
width: 400px;
height: 500px;
text-align: center;
top:100px;
}

.imagen {
height: 800px;
width: 800px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-a1">
<ul class="caption-style-1">
<li>
<img class="imagen" src="http://uploads5.wikiart.org/images/claude-monet/the-summer-poppy-field.jpg"/" alt="">
<div class="caption">
<div class="blur"></div>
<div class="caption-text">
<h1>Amazing Caption</h1>
<p>Whatever It Is - Always Awesome</p>
</div>
</div>
</li>
</ul>
</div>




Answer

Then, you just have to also add the height property of your image to your .blur class.

$(document).ready(function() {
    var imgWidth = $('.imagen').width();
    var imgHeight = $('.imagen').height();
    $('.caption-text').css({width: imgWidth});
  $('.caption-style-1').css({width: imgWidth});
  $('.blur').css({width: imgWidth, height: imgHeight});
});
.container-a1 {
  height: 100%;
  width: 100%;
}
.caption-style-1{
		list-style-type: none;
		margin: 0px;
		padding: 0px;
		
	}

	.caption-style-1 li{
		float: left;
		padding: 0px;
		position: relative;
		overflow: hidden;
	}

	.caption-style-1 li:hover .caption{
		opacity: 1;

	}


	.caption-style-1 img{
		margin: 0px;
		padding: 0px;
		float: left;
		z-index: 4;
	}


	.caption-style-1 .caption{
		cursor: pointer;
		position: absolute;
		opacity: 0;
		-webkit-transition:all 0.45s ease-in-out;
		-moz-transition:all 0.45s ease-in-out;
		-o-transition:all 0.45s ease-in-out;
		-ms-transition:all 0.45s ease-in-out;
		transition:all 0.45s ease-in-out;

	}
	.caption-style-1 .blur{
		background-color: rgba(0,0,0,0.65);
    position: absolute;
		height: 500px;
		width: 400px;
		z-index: 5;
		position: relative;
	}

	.caption-style-1 .caption-text h1{
		text-transform: uppercase;
		font-size: 24px;
	}
	.caption-style-1 .caption-text{
		z-index: 10;
		color: #fff;
		position: absolute;
		width: 400px;
		height: 500px;
		text-align: center;
		top:100px;
	}

.imagen {
  height: 800px;
  width: 800px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-a1">
		<ul class="caption-style-1">
			<li>
				<img class="imagen" src="http://uploads5.wikiart.org/images/claude-monet/the-summer-poppy-field.jpg"/" alt="">
				<div class="caption">
					<div class="blur"></div>
					<div class="caption-text">
						<h1>Amazing Caption</h1>
						<p>Whatever It Is - Always Awesome</p>
					</div>
				</div>
			</li>
    </ul>
</div>

Comments