我有一个盒子div,它有头部和主体。包含具有背景图像的div。
setInterval(function(){
var height = $(".box").height();
$(".box").height(height+2)
}, 1000);.box{
height: 100%;
background-color: #22ff2f;
width:350px;
}
.box-header{position:inherit; background-color:#9ed;width:100%;}
.box-body{}
.window{
background-position: center;
background-size: cover;
background-image: url('http://dailybestlike.com/wp-content/uploads/2015/07/simple-ideas-products-11.jpg');
min-height:190px;
width:100%;
height:100%;
position:relative;
text-align:center;
overflow:hidden;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="box">
<div class="box-header">
header
</div>
<div class="box-body">
<div class="window">
</div>
</div>
</div>

我的.box是一个可调整大小的方框,因此它将用鼠标调整大小
但是图像并不能拉伸盒子。我想用CSS属性来做这件事。我不想用javascript设置大小。这个是可能的吗?
发布于 2017-01-13 20:44:27
尝尝这个,
<div class="box">
<div class="box-header">
header
</div>
<div class="box-body">
<div class="window">
</div>
</div>
</div>JS代码:
setInterval(function() {
var height = $(".window").height();
$(".window").css({
'height': height + 100
});
}, 1000);此代码将拉伸您的图像。
发布于 2017-01-13 20:50:52
你需要为你的.box-header添加高度,感谢它,你将可以计算你的.box-body高度。现在,您在.window中的height:100%;将正常工作。
请检查以下内容:
setInterval(function(){
var height = $(".box").height();
$(".box").height(height+2)
}, 1000);.box{
height: 100%;
background-color: #22ff2f;
width:350px;
}
.box-header{
position:inherit;
background-color:#9ed;
width:100%;
height: 20px;
}
.box-body{
height: calc(100% - 20px);
}
.window{
background-position: center;
background-size: cover;
background-image: url('http://dailybestlike.com/wp-content/uploads/2015/07/simple-ideas-products-11.jpg');
min-height:190px;
width:100%;
height:100%;
position:relative;
text-align:center;
overflow:hidden;
}<div class="box">
<div class="box-header">
header
</div>
<div class="box-body">
<div class="window">
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
如果你想有一个动态的头部高度,你可以在javascript中动态计算它。
发布于 2017-01-13 20:53:07
不要改变.box高度如果你想要调整背景图片的大小,可以改变.window的高度!
https://jsfiddle.net/t87tjmmk/
setInterval(function(){
var height = $(".box").height();
$(".window").height(height+2); }, 1000);https://stackoverflow.com/questions/41634812
复制相似问题