Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调整大小时将图像高度应用于Div_Javascript_Jquery_Css - Fatal编程技术网

Javascript 调整大小时将图像高度应用于Div

Javascript 调整大小时将图像高度应用于Div,javascript,jquery,css,Javascript,Jquery,Css,我在一个div中有一个滑块。但是滑块的相对位置使滑块位于容器div的下方。我想通过获取滑块图像的高度并将其添加到相应div的class/id来增加div的高度 var img = $("#gallery > img"); $("#gallery").css({width:img.width(), height:img.height()}); HTML内容 <div id="gallery"> <img class="img-responsive" src="im

我在一个div中有一个滑块。但是滑块的相对位置使滑块位于容器div的下方。我想通过获取滑块图像的高度并将其添加到相应div的class/id来增加div的高度

var img = $("#gallery > img");
$("#gallery").css({width:img.width(), height:img.height()});
HTML内容

 <div id="gallery">

  <img class="img-responsive" src="images/shom/gallery/birdsview.jpg"/>
  <img class="img-responsive" src="images/shom/gallery/living.jpg"/>
  <img class="img-responsive" src="images/shom/gallery/bedroom.jpg"/>

</div>
<div class="col-md-12>Some description here</div>
上面你可以看到我使用的脚本。但问题是,当我调整浏览器窗口的大小时,高度没有相应地调整。当页面最初加载时,它仍然应用高度


期待您的支持。

处理这种情况的最佳解决方案是使用css,因为如果容器中有滑块,则容器的增长量至少应与其内容物的增长量相同,除非容器具有固定高度

但是现在,您可以通过在窗口调整大小时再次运行脚本来解决此问题

$( window ).resize(function() {
  var img = $("#gallery > img");
  $("#gallery").css({width:img.width(), height:img.height()});
});

当浏览器窗口的大小发生变化时,调整大小事件将发送到窗口元素。

发布HTML/CSS,否则我们很难解释为什么下面的div保持在容器顶部。抱歉。我现在已经更新了问题我运行了你的脚本。但它似乎不考虑高度或宽度。我们错过了什么吗。请建议。我尝试了CSS选项,但失败了。因此,考虑到通过scriptI将height应用于container div,我尝试了一个jsfiddle脚本,窗口大小调整工作正常,它触发了事件。尝试在内部添加一个console.log,并查看它是否被激发
$( window ).resize(function() {
  var img = $("#gallery > img");
  $("#gallery").css({width:img.width(), height:img.height()});
});