我有一个问题。我想在我的GET完全完成后做点什么,因为我需要把BG放在中间。此功能完全适用于调整大小,但是当我加载图像 onClick 时,这将不起作用。有什么办法可以避免这种情况?
例子:
getBG.php 返回 <img id="bgImage" src="123.jpg" />
问题:
它不会正确计算变量 a
因为我认为它甚至在加载图像之前就试图这样做。当图像完全加载时,它在 onResize 事件上工作得很好。
$.get("getBg.php", {
img: (this).id
},
function(data){
$("#bg").html(data);
var a = $("#bgImage").height() - $(window).height();
$("#bgImage").css("margin-top", - a / 2);
});
最佳答案
需要直接hook新图片的加载事件。 我假设 #bgImage 的元素是图像,这可能就是您想要的。
$.get("getBg.php", {
img: (this).id
},
function(data){
$("#bg").html(data);
$("#bgImage").load( function() {
var a = $(this).height() - $(window).height();
$(this).css("margin-top", - a / 2);
}
});
关于javascript - 当 $.get 完全完成时做一些事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586332/