jquery - 如何使我的悬停标题覆盖整个图像大小?

标签 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>

最佳答案

然后,您还需要将 imageheight 属性添加到您的 .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;
  }
<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>

关于jquery - 如何使我的悬停标题覆盖整个图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37438661/

相关文章:

html - CSS3 动画过渡 : opacity not working

html - 使 div 在 div 下等宽

jquery - 动态生成 kibana iframe(短网址)

php - 将 session 变量传递给 javascript

javascript - 如何将 HTML 字符串分割成多个片段并去掉外部标签?

javascript - SVG 图像 xlink :href is not working with knockout

javascript - 如何在jquery中检查父级

css - ASP.NET(脚本或 asp 之外的内容不受支持 :content)

html - 在 <p> 中将长文本分成多行

css - 单击时选择 div 中的所有内容?/调整文本框大小以适合文本?