javascript - 在 jQuery 中使用 JavaScript 返回值

标签 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('.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)
}

如您所见,我正在尝试输入一些值并返回 new_width 和 new_height。

感谢您的帮助!

最佳答案

如果您想一次返回两个值,您可以使用对象文字来完成...

return {
   width: new_width,
   height: new_height
};

关于javascript - 在 jQuery 中使用 JavaScript 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4811696/

相关文章:

javascript - 如何设置和读取cookie JavaScript

javascript - Ajax 发布未捕获的语法错误 : Unexpected token ILLEGAL

c - Scanf 在 C 中无法正确读取字符串。出了什么问题?

javascript - 如何以 Angular 滚动到底部或将焦点移动到底部?

c - 如何编写使用其他函数的值的函数

python - 如何在 Python 3 中调用函数之前停止执行

javascript - 使用 AJAX 将模态表单数据发布到 php

javascript - 即使值没有改变也会触发 onChange

javascript - 使用 foreach 循环增加重复项的可能性

javascript - JS/jQuery : fire onchange from a disabled/readonly input without using button