Javascript 附加到div图像并计算它';s高度并设置为css
我有这样的js(我也使用jQuery): 但当我尝试获取图像大小时,我看到它的高度(根据我的代码)是0Javascript 附加到div图像并计算它';s高度并设置为css,javascript,jquery,Javascript,Jquery,我有这样的js(我也使用jQuery): 但当我尝试获取图像大小时,我看到它的高度(根据我的代码)是0 如何附加、获取和设置css图像大小?我的代码不起作用加载图像后需要获取高度 $(".article_big_photo").click(function() { $('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()}); $('#screen').sh
如何附加、获取和设置css图像大小?我的代码不起作用加载图像后需要获取高度
$(".article_big_photo").click(function() {
$('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()});
$('#screen').show();
$('#article_photo_modal').show();
var image = document.createElement("img");
var url = $(this).attr("b_img");
$(image).attr("src", url);
$(image).load(function(){
$('#article_photo_modal').css({ 'margin':$(image).height() +" 0 0 -270px"});
});
$('.photo_innerbox').append(image);
});
加载图像后,需要获取高度
$(".article_big_photo").click(function() {
$('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()});
$('#screen').show();
$('#article_photo_modal').show();
var image = document.createElement("img");
var url = $(this).attr("b_img");
$(image).attr("src", url);
$(image).load(function(){
$('#article_photo_modal').css({ 'margin':$(image).height() +" 0 0 -270px"});
});
$('.photo_innerbox').append(image);
});
就像Arun说的,你必须在图像加载后获取高度 您也可以尝试以下方法:
var img = new Image();
img.src = $(this).attr("b_img");
img.height;
就像Arun说的,你必须在图像加载后获取高度 您也可以尝试以下方法:
var img = new Image();
img.src = $(this).attr("b_img");
img.height;
可能的重复可能的重复