让我们直面我的问题:
这是我的代码:
function inside()
{
var gallery = $("#gallery");
var photo = $("#photo");
var width_mask = gallery.width(); //defines width for mask
var height_mask = gallery.height(); //defines height for mask
var img = new Image();
img.onload = function() {
var width_image = this.width;
var height_image = this.height;
var img_src = img.src;
if((width_image/width_mask)>=(height_image/height_mask))
{
height_image = ((width_mask/width_image)*height_image);
width_image = width_mask;
}
else if((width_image/width_mask)<(height_image/height_mask))
{
width_image = ((height_mask/height_image)*width_image);
height_image = height_mask;
}
var top_margin = (height_mask - height_image)/2;
var left_margin = (width_mask-width_image)/2;
photo.css({
marginTop : top_margin,
marginLeft: left_margin,
marginRight: left_margin
});
photo.attr('src',img_src);
photo.attr('width',width_image);
photo.attr('height',height_image);
gallery.css({
width : width_mask,
height : height_mask
});
};
img.src = photo.attr('src');
}
好的,正如您所看到的,这是我的函数......这是我的问题:如何在 img.onload 函数中返回“top_margin”和“left_margin”变量?
好吧,实际上我知道我们如何在函数中返回变量,但是在这个 onload 函数中,它似乎不起作用:(
对不起,我是 Javascript 的一个小初学者......任何帮助都会非常感激。
非常感谢, Naghme