javascript - 如何按比例调整图像大小/保持纵横比?

标签 javascript jquery image resize

我的图片尺寸会很大,我想用 jQuery 缩小它们,同时保持比例受限,即相同的纵横比。

谁能给我指出一些代码,或者解释一下逻辑?

最佳答案

我认为这是一个真正的cool method :

 /**
  * Conserve aspect ratio of the original region. Useful when shrinking/enlarging
  * images to fit into a certain area.
  *
  * @param {Number} srcWidth width of source image
  * @param {Number} srcHeight height of source image
  * @param {Number} maxWidth maximum available width
  * @param {Number} maxHeight maximum available height
  * @return {Object} { width, height }
  */
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {

    var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);

    return { width: srcWidth*ratio, height: srcHeight*ratio };
 }

关于javascript - 如何按比例调整图像大小/保持纵横比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978209/

相关文章:

javascript - 当使用 JavaScript 验证输入字段时,使用 form_tag 时,提交按钮在 Rails 中停止工作

jquery - Angular JS : temporary disable animation provided by directive so it doesn't animate when the page load?

ios - 在异步图像加载到 UITableViewCell 后滚动时,Swift 图像更改为错误的图像

iphone - 执行没有角点的透视变换

javascript - 如何读取本地json文件并将其存储在变量中?

javascript - 如何对一堆 <path> 进行分组

Jquery ajax响应数据插入相同的div id而不是基于复选框id

javascript - 谷歌地图 setCenter(),v3,当单击元素时,使用 jQuery

如果日期是 01,JavaScript 日期返回错误的月份

android - 什么是创建 Android 动画的更好方法?