Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery中的img.onload=function()内返回变量?_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何在jquery中的img.onload=function()内返回变量?

Javascript 如何在jquery中的img.onload=function()内返回变量?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,让我们直截了当地回答我的问题: 这是我的密码: 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

让我们直截了当地回答我的问题:

这是我的密码:

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');
}
函数内部()
{
var画廊=$(“#画廊”);
var photo=$(“#photo”);
var width_mask=gallery.width();//定义掩码的宽度
var height_mask=gallery.height();//定义掩码的高度
var img=新图像();
img.onload=函数(){
var width_image=this.width;
var height_image=this.height;
var img_src=img.src;
如果((宽度图像/宽度遮罩)>=(高度图像/高度遮罩))
{
高度图像=((宽度遮罩/宽度图像)*高度图像);
宽度\图像=宽度\遮罩;
}
else if((width\u image/width\u mask)不能从onload“返回值”,因为它是一个异步回调

可以,但是没有意义,因为是浏览器调用onload()回调,而对其返回值不感兴趣

Asynchronous=不会立即执行,而是在将来的某个时候执行

同步=将立即执行

异步性是通过回调来处理的。回调是一种机制,通过将函数传递给另一个函数来工作,因此被调用函数可以在将来的某个时间通知调用方代码正在完成的工作

如果函数同步工作,它可以简单地返回值而无需回调。但缺点是调用函数的代码必须等待从服务器加载映像,这可能需要很长时间,如果服务器的响应需要很长时间,则会使程序长时间冻结.你不想那样做

如果像这样调用inside()函数:

inside();
您可以从onload返回一个值(或任何内容),只要异步执行即可。您可以通过进行以下修改来执行此操作:

function inside(imageLoadedCb) {
    // ...

    img.onload = function () {
        // ...

        // notify the caller of inside() that image was loaded, with the values we want to return
        imageLoadedCb(top_margin, left_margin);
    }
}

inside(
    // this is a function reference. it ends up in "imageLoadedCb" variable in "inside" function
    function (top_margin, left_margin){
        // this code gets invoked from inside the onload() callback
        console.log('top_margin= ' + top_margin + ' left_margin= ' + left_margin);
    }
);

该函数是异步的。您试图如何处理这些变量?