[英]Resize div with jquery javascript and stretch background image
I have a box div and it has header and body. 我有一个框div,它具有标题和正文。 Contains a div that has a background image. 包含具有背景图片的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>
my .box
is a resizable box, so it will be resize with mouse 我的.box
是可调整大小的框,因此可以使用鼠标调整大小
But image does not stretch the box. 但是图像不能拉伸盒子。 I want to do this with CSS properties. 我想用CSS属性来做到这一点。 I do not want to set size with javascript. 我不想使用javascript设置大小。 Is this possible? 这可能吗?
Try this, 尝试这个,
<div class="box">
<div class="box-header">
header
</div>
<div class="box-body">
<div class="window">
</div>
</div>
</div>
JS CODE : JS代码:
setInterval(function() {
var height = $(".window").height();
$(".window").css({
'height': height + 100
});
}, 1000);
This code will stretch your image. 此代码将拉伸您的图像。
You need to add height for your .box-header
, and thanks it you will be can calc your .box-body
height. 您需要为.box-header
添加高度,并感谢您可以计算出.box-body
高度。 Now your height:100%;
现在您的height:100%;
in .window
will be work properly. 在.window
中将正常工作。
Check this: 检查一下:
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>
If you want to have a dynamic height of the header, you can dynamically calculate it in javascript. 如果要使标题具有动态高度,可以在javascript中动态计算它。
Don't change .box height if you want the background image to resize, change .window height! 如果要调整背景图像的大小,请不要更改.box的高度,请更改.window的高度!
https://jsfiddle.net/t87tjmmk/ https://jsfiddle.net/t87tjmmk/
setInterval(function(){
var height = $(".box").height();
$(".window").height(height+2); }, 1000);
I achieved what you want here. 我在这里实现了你想要的。 See Demo here 在这里查看演示
HTML HTML
<div class="box">
<div class="box-header">
header
</div>
<div class="box-body">
<div id='w' class="window">
</div>
</div>
</div>
JavaScript JavaScript的
var w=document.getElementById('w');
var x=192;
setInterval(function(){
var height = $(".box").height();
$(".box").height(height+2);
w.style.minHeight=x+"px";
x+=2;
},90);
CSS CSS
.box{
height: 100%;
background-color: #22ff2f;
width:350px;
}
.box-header{position:inherit; background-color:#9ed;width:100%;}
.box-body{}
.window{
background-position: center;
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;
background-repeat:no-repeat;
background-size: 100% 100%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.