[英]Append to div image and calculate it's height and set to css
I have such js (also i use jQuery): 我有这样的js(我也使用jQuery):
$(".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);
$('#article_photo_modal').css({ 'margin':$(image).height()+" 0 0 -270px"});
$('.photo_innerbox').append(image);
});
but when i try to get image size, i see that it's height (according my code) is 0. 但是当我试图获得图像大小时,我发现它的高度(根据我的代码)是0。
How can i append, get, and set to css image size? 我如何追加,获取和设置为css图像大小? My code didn't work 我的代码不起作用
you need to fetch the height after the image is loaded 你需要在加载图像后获取高度
$(".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);
});
Like Arun said you must fetch the height after image load. 就像Arun所说,你必须在图像加载后获取高度。
You can also try this: 你也可以试试这个:
var img = new Image();
img.src = $(this).attr("b_img");
img.height;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.