Js Math對象 abs() 方法


語法

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); }; }; }); }

資源搜索網站大全 https://www.renrenfan.com.cn 廣州VI設計公司https://www.houdianzi.com

關鍵代碼

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;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM