一些公用的JS
Posted 黑色白沙的园子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一些公用的JS相关的知识,希望对你有一定的参考价值。
/*阻止冒泡*/ function preventBubble(event){ var e=arguments.callee.caller.arguments[0]||event; //若省略此句,下面的e改为event,IE运行可以,但是其他浏览器就不兼容 if (e && e.stopPropagation) { e.stopPropagation(); } else if (window.event) { window.event.cancelBubble = true; } } /*页面跳转*/ function go_page(url){ location.href = url } /*数组中按值删除*/ function removeByValue(arr, val) { for(var i=0; i<arr.length; i++) { if(arr[i] == val) { arr.splice(i, 1); break; } } return arr } /*数组去重*/ function ArrayClearRepeat(arr){ var new_arr=[]; for(var i=0;i<arr.length;i++) { var items=arr[i]; //判断元素是否存在于new_arr中,如果不存在则插入到new_ar中 if($.inArray(items,new_arr)==-1) { new_arr.push(items); } } return new_arr } /*随机数生成*/ function randomString(len) { len = len || 32; var $chars = ‘ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678‘; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ var maxPos = $chars.length; var pwd = ‘‘; for (i = 0; i < len; i++) { pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; } function isNum(the_str){ var is_val = parseInt(the_str);//如果变量val是字符类型的数则转换为int类型 如果不是则is_val为NaN if(!isNaN(is_val)){ return true } else{ return false } } function getLocalTime(i) { //参数i为时区值数字,比如北京为东八区则输进8,西5输入-5 if (typeof i !== ‘number‘) return; var d = new Date(); //得到1970年一月一日到现在的秒数 var len = d.getTime(); //本地时间与GMT时间的时间偏移差 var offset = d.getTimezoneOffset() * 60000; //得到现在的格林尼治时间 var utcTime = len + offset; return new Date(utcTime + 3600000 * i); } /** * 带滚动条抬头和内容对齐
/*判断两个数组是否相同*/ sameArray: function(arr1, arr2) { if (arr1.length == arr2.length) { var arr3 = []; for (var key in arr1) { var stra = arr1[key]; var count = 0; for (var j = 0; j < arr2.length; j++) { var strb = arr2[j]; if (stra == strb) { count++; } } if (count === 0) { //表示数组1的这个值没有重复的,放到arr3列表中 arr3.push(stra); } } if (arr3 != []) { return true } else { return false } } else { return false } }, // beta64方法 getBase64: function(file, callback) { var maxWidth = 640; if (file.files && file.files[0]) { var thisFile = file.files[0]; if (thisFile.size > 2019200) { // mualert.alertBox("图片不能超过800K"); alert("2M以内の画像を選択してください!"); return }; var reader = new FileReader(); reader.onload = function(event) { var imgUrl = event.target.result; var img = new Image(); img.onload = function() { var canvasId = ‘canvasBase64Imgid‘, canvas = document.getElementById(canvasId); if (canvas != null) { document.body.removeChild(canvas); } var canvas = document.createElement("canvas"); canvas.innerhtml = ‘New Canvas‘; canvas.setAttribute("id", canvasId); canvas.style.display = ‘none‘; document.body.appendChild(canvas); canvas.width = this.width; canvas.height = this.height; var imageWidth = this.width, imageHeight = this.height; if (this.width > maxWidth) { imageWidth = maxWidth; imageHeight = this.height * maxWidth / this.width; canvas.width = imageWidth; canvas.height = imageHeight; } var context = canvas.getContext(‘2d‘); context.clearRect(0, 0, imageWidth, imageHeight); context.drawImage(this, 0, 0, imageWidth, imageHeight); var base64 = canvas.toDataURL(‘image/png‘, 1); var imgbase = base64.substr(22); callback(imgbase) //this.imgUrl = } img.src = imgUrl; } reader.readAsDataURL(file.files[0]); } }, // 数组比较,输出不同,A数组为max数组 array_comparison: function(array, array2) { var arr3 = []; for (key in array) { var stra = array[key]; var count = 0; for (var j = 0; j < array2.length; j++) { var strb = array2[j]; if (stra == strb) { count++; } } if (count === 0) { //表示数组1的这个值没有重复的,放到arr3列表中 arr3.push(stra); } } return arr3 }, // Sort_by_date:function(data_list,orderType){ for(var index in data_list){ let time_arr = data_list[index].date.split(".") let time_nums = time_arr.length let times = null if(time_nums==1){ let year = time_arr[0] times = Number(year)*10000 } if(time_nums==2){ let year = time_arr[0] let mouth = time_arr[1] if(mouth<10){ mouth = ‘0‘ + mouth } times = Number(year)*10000 + Number(mouth)*100 } if(time_nums==3){ let year = time_arr[0] let mouth = time_arr[1] let day = time_arr[2] if(mouth<10){ mouth = ‘0‘ + mouth } if(day<10){ day = ‘0‘ + day } times = Number(year)*10000 + Number(mouth)*100 + Number(day)*1 } data_list[index][‘temp_date_num‘] = times } if (orderType!==0){ data_list.sort(function (p1,p2) { //如果返回负数,p1在前,返回正数,p2在前 //1代表升序,2代表降序 if (orderType===2){ return p2.temp_date_num-p1.temp_date_num; } else { return p1.temp_date_num-p2.temp_date_num; } }) } return data_list; }, Sort_by_date_mouth:function(data_list,orderType){ for(var index in data_list){ let time_arr = data_list[index].year_month.split(".") let year = time_arr[0] let mouth = time_arr[1] if(mouth<10){ mouth = ‘0‘ + mouth } let times = Number(year)*10000 + Number(mouth)*100 data_list[index][‘temp_date_num‘] = times } if (orderType!==0){ data_list.sort(function (p1,p2) { //如果返回负数,p1在前,返回正数,p2在前 //1代表升序,2代表降序 if (orderType===2){ return p2.temp_date_num-p1.temp_date_num; } else { return p1.temp_date_num-p2.temp_date_num; } }) } return data_list; }, //对数组进行去重 Array_deduplication: function(arr){ var new_arr=[]; for(var i=0;i<arr.length;i++) { var items=arr[i]; //判断元素是否存在于new_arr中,如果不存在则插入到new_arr的最后 if($.inArray(items,new_arr)==-1) { new_arr.push(items); } } new_arr = new_arr.sort(this.sortNumber); return new_arr }, //对数组进行排序 sortNumber:function(a,b) { return a - b; }, // searchList:function(str, container){ var newList = []; //新的列表 var startChar = str.charAt(0); //开始字符 var strLen = str.length; //查找符串的长度 for (var i = 0; i < container.length; i++) { var obj = container[i]; var isMatch = false; for (var p in obj) { if (typeof (obj[p]) == "function") { obj[p](); } else { var curItem = ""; if (obj[p] != null) { curItem = obj[p]; } for (var j = 0; j < curItem.length; j++) { if (curItem.charAt(j) == startChar) { if (curItem.substring(j).substring(0, strLen) == str) { isMatch = true; break; } } } } } if (isMatch) { newList.push(obj); } } return newList; }, // js sort方法根据数组中对象的某一个属性值进行排序 arr.sort(compare_list_by_key(‘age‘)) compare_list_by_key:function(property){ return function(a,b){ var value1 = a[property]; var value2 = b[property]; return value1 - value2; } }, getLocalTime:function (i) { //参数i为时区值数字,比如北京为东八区则输进8,西5输入-5 if (typeof i !== ‘number‘) return; var d = new Date(); //得到1970年一月一日到现在的秒数 var len = d.getTime(); //本地时间与GMT时间的时间偏移差 var offset = d.getTimezoneOffset() * 60000; //得到现在的格林尼治时间 var utcTime = len + offset; return new Date(utcTime + 3600000 * i); }, GetDay:function(i){ i = !i ? 0 : i; var day1 = new Date(); day1.setTime(day1.getTime()+i*(24*60*60*1000)); let year = day1.getFullYear() let month = (day1.getMonth()+1) month = month < 10 ? "0"+month : month; let days = day1.getDate() var s1 = year + "-" + month + "-" + days; return s1 }, DateDiff:function(sDate1, sDate2) { //sDate1和sDate2是yyyy-MM-dd格式 var aDate, oDate1, oDate2, iDays; aDate = sDate1.split("-"); oDate1 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0]); //转换为yyyy-MM-dd格式 aDate = sDate2.split("-"); oDate2 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0]); iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数 return iDays; //返回相差天数 }, // 字典是否在一个数组中 {} in [] 按照关键字判断 dict_in_array:function(_dict, _array, _field){ var _init = false for(var index in _array){ if(_array[index][_field]==_dict[_field]){ _init = true } } return _init }
*/ function Scroll_bar_widening(ul,title){ setTimeout(() => { $("."+ul).scrollTop(10);//控制滚动条下移10px if( $("."+ul).scrollTop()>0 ){ $("."+title).css({ "padding-right": ‘17px‘ }) }else{ $("."+title).css({ "padding-right": ‘0‘ }) } $("."+ul).scrollTop(0);//滚动条返回顶部 }, 100); }
以上是关于一些公用的JS的主要内容,如果未能解决你的问题,请参考以下文章