1. 程式人生 > >實現自己的js工具庫(持續更新)

實現自己的js工具庫(持續更新)

(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端 if (isAndroid) { return 'Android'; } else if (isiOS) { return 'iOS'; } else { return 'others'; } }, /** * 四捨五入強制保留n位小數 * @param {Number}
x 運算元字 * @param {Number} n 保留位數 */ toDecimal(x, n) { var f = parseFloat(x); if (isNaN(f) && isNaN(n)) { return false; } if (n === 0) return Math.round(x
)
; var num = Number("1E" + n); var f = Math.round(x * num) / num; var s = f.toString(); var rs = s.indexOf("."); if (rs < 0) { rs = s.length; s += ".";
}
while (s.length <= rs + n) { s += "0"; } return s; }
, /** * 判斷兩個物件是否相等 * @param {Object} x 物件1 * @param {Object} y 物件2 */ equals: function(x, y) { var in1 = x instanceof Object; var in2 = y instanceof Object; if(!in1 || !in2) { return x === y; } if(Object.keys(x).length !== Object.keys(y).length) { return false; } for(var p in x) { var a = x[p] instanceof Object; var b = y[p] instanceof Object; if(a && b) { return this.equals(x[p], y[p]); } else if(x[p] !== y[p]) { return false; } } return true; }, /** * 獲取日期時間 格式 "yyyy-MM-dd HH:MM" * @param {Data} data 可選 指定時間 */ getNowDate: function(data) { var date = data || new Date(); var month = date.getMonth() + 1; var strDate = date.getDate(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); month >= 1 && month <= 9 ? month = "0" + month : ''; strDate >= 0 && strDate <= 9 ? strDate = "0" + strDate : ''; minutes >= 0 && minutes <= 9 ? minutes = "0" + minutes : ''; seconds >= 0 && seconds <= 9 ? seconds = "0" + seconds : ''; var currentdate = date.getFullYear() + "-" + month + "-" + strDate + " " + date.getHours() + ":" + minutes + ":" + seconds; return currentdate; }, /** * 計算兩個時間差 * @param {Data} startTime 開始時間(xxxx-xx-xx) * @param {Data} endTime 結束時間(xxxx-xx-xx) * return xx年xx天 || xx天xx小時 || xx小時xx分 */ getDateDiff: function(startTime, endTime) { //將xxxx-xx-xx的時間格式,轉換為 xxxx/xx/xx的格式 startTime = startTime.replace(/\-/g, "/"); endTime = endTime.replace(/\-/g, "/"); var sTime = new Date(startTime); //開始時間 var eTime = new Date(endTime); //結束時間 var timeOff = eTime - sTime; //相差時間戳(毫秒數) var timeMinute = 1000 * 60; var timeHour = 1000 * 3600; var timeDay = 1000 * 3600 * 24; var timeYear = 1000 * 3600 * 24 * 365; if(timeOff / timeYear >= 1) { return parseInt(timeOff / timeYear) + "年" + parseInt((timeOff % timeYear)/timeDay) + "天"; } else if(timeOff / timeDay >= 1) { return parseInt(timeOff / timeDay) + "天" + parseInt((timeOff % timeDay)/timeHour) + "小時"; } else { return parseInt(timeOff / timeHour) + "小時" + parseInt((timeOff % timeHour)/timeMinute) + "分"; } }, /*  * 圖片壓縮,默認同比例壓縮  * @param {Object} path  *   pc端傳入的路徑可以為相對路徑,但是在移動端上必須傳入的路徑是照相圖片儲存的絕對路徑  * @param {Object} obj  *   obj 物件 有 width, height, quality(0-1)  * @param {Object} callback  *   回撥函式有一個引數,base64的字串資料 * 呼叫示例 yui.compressImg('../img/time.jpg',{width:100,height:100,quality:0.8},function(res){ console.log(res);//base64的字串資料 });  */ compressImg: function(path, obj, callback){ var img = new Image(); img.src = path; img.onload = function(){ var that = this; // 預設按比例壓縮 var w = that.width, h = that.height,     scale = w / h;     w = obj.width || w;     h = obj.height || (w / scale); var quality = 0.7;  // 預設圖片質量為0.7 //生成canvas var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); // 建立屬性節點 var anw = document.createAttribute("width"); anw.nodeValue = w; var anh = document.createAttribute("height"); anh.nodeValue = h; canvas.setAttributeNode(anw); canvas.setAttributeNode(anh); ctx.drawImage(that, 0, 0, w, h); // 影象質量 if(obj.quality && obj.quality <= 1 && obj.quality > 0){  quality = obj.quality; } // quality值越小,所繪製出的影象越模糊 var base64 = canvas.toDataURL('image/jpeg', quality ); // 回撥函式返回base64的值 callback(base64); }; } } window.yui = yui; })