我正在使用一个函数,该函数使用图像来设置div的margin-top
。
它会找到图像高度和窗口高度,并将div边距顶部设置为这两个值中较小的一个。它适用于所有本地浏览器(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
发布于 2015-01-31 23:22:30
所以我想我想通了。您必须将该函数包装在一个window.load函数中,以确保所有图像都已加载。据我所知,这不适用于动态添加的图像。下面是修复它的代码:
//make sure images have loaded before running image height
$(window).load(function() {
portfolioMargin();
});
https://stackoverflow.com/questions/28252964
复制相似问题