语法
Math.abs(x) // 必需。必须是一个数值。
示例
<script type="text/JavaScript">
document.write(Math.abs(7.25) + "<br />")
document.write(Math.abs(-7.25) + "<br />")
document.write(Math.abs(7.25-10))
</script>
输出:
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 = {
width: 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);
};
};
});
}
关键代码
reader.onload = function(_file) {
image.src = _file.target.result;
image.onload = function() {
var meta = {
width: 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;