我正在使用一个使用图片设置div margin-top
的函数。
找到图像高度和窗口高度,并将divs margin-top设置为两个值中较小的一个。它适用于所有本地浏览器(IE,Chrome,Firefox)。
当我将其上传到GitHub时,它无法识别图像高度。现在,当我在GitHub上运行下面的代码时,图像高度的console.log
为0
。当我在本地运行时,图像高度的console.log
是正确的。图像加载到GitHub上,因此不会出现没有进入网页的图像问题。
似乎脚本在GitHub上加载图像之前运行,但不在本地加载。有人有主意吗?
var portfolioMargin = function() {
var windowHeight = window.innerHeight;
var imageHeight = $('#imageWrap img').height();
//if windowheight is bigger than image height make portfolio div margin equal to image height, else do the opposite
console.log("window height " + windowHeight);
console.log("image height " + imageHeight);
if (windowHeight > imageHeight) {
$('#portfolio').css('margin-top', imageHeight);
} else {
$('#portfolio').css('margin-top', windowHeight);
}
};//end portfolioMargin function

答案 0 :(得分:0)
所以我想出来了。您必须将函数包装在window.load函数中以确保已加载所有图像。据我所知,这对于动态添加的图像不起作用。以下是修复它的代码:
//make sure images have loaded before running image height
$(window).load(function() {
portfolioMargin();
});