Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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
在jQuery中使用JavaScript返回值_Javascript_Jquery_Function_Resize_Image Resizing - Fatal编程技术网

在jQuery中使用JavaScript返回值

在jQuery中使用JavaScript返回值,javascript,jquery,function,resize,image-resizing,Javascript,Jquery,Function,Resize,Image Resizing,嘿,伙计们。 所以这更多的是关于我不了解JS,而不是一个真正的难题 我正在尝试用JS编写一个小函数,我可以从jQuery调用它来调整图像的大小。问题是我不知道在JS函数运行后如何将值返回给jQuery。以下是我到目前为止的情况: $('a').click(function() { var slideshowHeight = $(this).closest('.stage').css('height') var slideshowWidth = $(this).closest('.

嘿,伙计们。 所以这更多的是关于我不了解JS,而不是一个真正的难题

我正在尝试用JS编写一个小函数,我可以从jQuery调用它来调整图像的大小。问题是我不知道在JS函数运行后如何将值返回给jQuery。以下是我到目前为止的情况:

$('a').click(function() {
    var slideshowHeight = $(this).closest('.stage').css('height')
    var slideshowWidth = $(this).closest('.stage').css('width')
    var newImageHeight = $(this).attr('data-height')
    var newImageWidth = $(this).attr('data-width')

    fit_within_box(slideshowWidth, slideshowHeight, newImageWidth, newImageHeight);

    $(this).children('img').css('width',new_width);
    $(this).children('img').css('width',new_height);
}

function fit_within_box(box_width, box_height, width, height)
{
    var new_width = width
    var new_height = height
    var aspect_ratio = parseInt(width) / parseInt(height)

    if (new_width > box_width)
    {
        new_width = box_width
        new_height = int(new_width / aspect_ratio)
    }

    if (new_height > box_height)
    {
        new_height = box_height
        new_width = int(new_height * aspect_ratio)
    }
    return (new_width, new_height)
}
正如你们所看到的,我试图输入一些值,并得到新的宽度和高度


谢谢你的帮助

如果要同时返回这两个值,可以使用对象文本

return {
   width: new_width,
   height: new_height
};

如果您想同时返回这两个值,您可以使用对象文本

return {
   width: new_width,
   height: new_height
};

您可以尝试如下方式返回JSON对象:

return {
    new_height:height,
    new_width:width
};
并在jquery函数中使用它:

var fit_img = fit_within_box(...);
...
$(this).children('img').css('height',fit_img.new_height);

您可以尝试如下方式返回JSON对象:

return {
    new_height:height,
    new_width:width
};
并在jquery函数中使用它:

var fit_img = fit_within_box(...);
...
$(this).children('img').css('height',fit_img.new_height);

嘿,这是一个工作示例,我已经修复了一些小错误,例如,您在
children('img')上使用了两次宽度

jQuery:

$(function(){
    $('a').click(function() {
        var slideshowHeight=$(this).closest('.stage').height();
        var slideshowWidth=$(this).closest('.stage').width();
        var newImageHeight=$(this).attr('data-height');
        var newImageWidth=$(this).attr('data-width');
        var size=fit_within_box(slideshowWidth,slideshowHeight,newImageWidth,newImageHeight);
        $(this).children('img').css({'width':size[0],'height':size[1]});
    });

    function fit_within_box(box_width,box_height,new_width,new_height){
        var aspect_ratio=new_width/new_height;
        if(new_width>box_width){
            new_width=box_width;
            new_height=Math.round(new_width/aspect_ratio);
        }
        if(new_height>box_height){
            new_height=box_height;
            new_width=Math.round(new_height*aspect_ratio);
        }
        return [new_width,new_height];
    }
});
带有一些壁纸的HTML:

<div class="stage" style="width:200px;height:200px;">
<a href="#" data-width="1280" data-height="1024"><img src="wallpaper_1280x1024.jpg" /></a>
</div>

干杯


G.

嘿,这是一个工作示例,我已经修复了一些小错误,例如,您在
children('img')上使用了两次宽度

jQuery:

$(function(){
    $('a').click(function() {
        var slideshowHeight=$(this).closest('.stage').height();
        var slideshowWidth=$(this).closest('.stage').width();
        var newImageHeight=$(this).attr('data-height');
        var newImageWidth=$(this).attr('data-width');
        var size=fit_within_box(slideshowWidth,slideshowHeight,newImageWidth,newImageHeight);
        $(this).children('img').css({'width':size[0],'height':size[1]});
    });

    function fit_within_box(box_width,box_height,new_width,new_height){
        var aspect_ratio=new_width/new_height;
        if(new_width>box_width){
            new_width=box_width;
            new_height=Math.round(new_width/aspect_ratio);
        }
        if(new_height>box_height){
            new_height=box_height;
            new_width=Math.round(new_height*aspect_ratio);
        }
        return [new_width,new_height];
    }
});
带有一些壁纸的HTML:

<div class="stage" style="width:200px;height:200px;">
<a href="#" data-width="1280" data-height="1024"><img src="wallpaper_1280x1024.jpg" /></a>
</div>

干杯


是的,这是个好主意。我如何在jQuery中访问它?我猜是这样的:resize=fit_in_box(a,b,x,y)resize.width resize.height这看起来对吗?
fit_in_box
函数的值也需要在某个地方捕获。关于OP代码中的语法错误的注释,
return(1,2,…,n)
只返回
n
,因此OP只能从函数中返回
new\u height
。@Drew Baker,别忘了你的
var
关键字。是的,这是个好主意。我如何在jQuery中访问它?我猜是这样的:resize=fit_in_box(a,b,x,y)resize.width resize.height这看起来对吗?
fit_in_box
函数的值也需要在某个地方捕获。关于OP代码中的语法错误的注释,
return(1,2,…,n)
只返回
n
,因此OP只会从函数中返回
new\u height
。@Drew Baker,别忘了你的
var
关键字。那不是JSON对象,只是一个对象文本。那不是JSON对象,只是一个对象文本。