Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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
Jquery 使用image height()设置另一个元素';s边距_Jquery_Height - Fatal编程技术网

Jquery 使用image height()设置另一个元素';s边距

Jquery 使用image height()设置另一个元素';s边距,jquery,height,Jquery,Height,我试图使一个DIV的上边距等于另一个元素的高度 我无法让它在Chrome中工作。在FF和IE9中似乎很好 我使用height()来获取图像的高度 然后,我从另一个DIV的上边距中减去该值 var GalleryImgHeight = $("#container #gallery img").height(); $("#content_container").css("margin-top", - GalleryImgHeight); <div id="container"> <

我试图使一个DIV的上边距等于另一个元素的高度

我无法让它在Chrome中工作。在FF和IE9中似乎很好

我使用height()来获取图像的高度

然后,我从另一个DIV的上边距中减去该值

var GalleryImgHeight = $("#container #gallery img").height();
$("#content_container").css("margin-top", - GalleryImgHeight);

<div id="container">
<div id="gallery"><img src="images/1.jpg" alt=""></div>
<div id="content_container">Lorem ipsum dolor sit amet, consectetuer.......</div>
</div>
var gallerymgheight=$(“#容器#画廊img”).height();
$(“#内容容器”).css(“页边空白顶部”,-GalleryMgHeight);
洛雷姆·伊普苏姆·多洛·希特·阿梅特,康塞特图尔。。。。。。。
我在resize函数中有相同的代码,因此如果它们调整浏览器的大小,它会重新计算

对于Chrome,它只适用于调整大小。我猜初始函数是在加载图像之前获取高度

我没有收到任何错误

我应该尝试预加载图像吗

以下是完整的代码:

我这样做的原因是让一个DIV叠加在另一个DIV之上。两个DIV都相对放置,因为底部DIV的宽度为100%,因此其尺寸将取决于浏览器大小。如果我错了,请告诉我,但我无法将top DIV的位置设置为绝对,因为我希望它在浏览器中居中。因此,我无法将“left”属性设置为静态值


Mabye我采取了错误的方法,但我很想听到另一种看法。

Gordan的回答很好,但需要对代码进行更改,以使以下行正常工作:

var origMarginTop = $("#content_container").css("margin-top");
因为它返回一个完整css值的字符串,即“10px”。很明显,你不能从10px得到一个纯数字,你需要它只有10。(例如10px-180不起作用,10-180会起作用)

为了实现这一点,我建议使用以下几行(增加1行,更改第3行):

对代码的这种修改对我来说很好

var marginTopString = origMarginTop.slice(0, -2);

if (GalleryImgHeight !== 0) {
$("#content_container").css("margin-top", marginTopString - GalleryImgHeight);