Js Math对象 abs() 方法

语法

Math.abs(x) // 必需。必须是一个数值。

示例

输出:

7.25
7.25
2.75

在项目中使用的方法 上传图片

Init: function(up, file) {
let alertMsg = cbFilesAddedParam.alertMsg;
plupload.addFileFilter("img_ratio", function(ratio, file, cb) {
if (file.type.indexOf("image") == -1) return cb(false);
var originFile = file.getNative();
var reader = new FileReader();
var image = new Image();
reader.readAsDataURL(originFile);
reader.onload = function(_file) {
image.src = _file.target.result;
image.onload = function() {
var meta = {
this.width,
height: this.height,
src: this.src
};
var compat = Math.abs((meta.width / meta.height) - ratio.ratio) < 0.01;
cb(compat);
compat || layer.alert(ratio.alertMsg);
};
};
});
}
资源搜索网站大全 https://www.renrenfan.com.cn 广州VI设计公司https://www.houdianzi.com

关键代码

reader.onload = function(_file) {
image.src = _file.target.result;
image.onload = function() {
var meta = {
this.width,
height: this.height,
src: this.src
};
var compat = Math.abs((meta.width / meta.height) - ratio.ratio) < 0.01;
cb(compat);
compat || layer.alert(ratio.alertMsg);
};
};

精确代码

var compat = Math.abs((meta.width / meta.height) - ratio.ratio) < 0.01;

原文地址:https://www.cnblogs.com/wwyydd/p/14356091.html