如何使我的悬停字幕覆盖整个图像大小?

时间:2016-05-25 13:27:03

标签: jquery html css hover caption

我尝试制作一个悬浮字幕,即使调整大小,它也可以适应整个图像的宽度和高度。我尝试过使用我在这里找到的jquery代码,但是我还没有弄清楚如何让它工作。



$(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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

然后,您还必须将height的{​​{1}}属性添加到image课程中。

.blur
$(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;
  }