常用的功能封装 pool.js
Posted 慕东
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用的功能封装 pool.js相关的知识,希望对你有一定的参考价值。
//import { setInterval } from "timers"; //获取最大值 function getMax(){ var max = arguments[0]; for(var i=0;i<arguments.length;i++){ if(max<=arguments[i]){ max=arguments[i] } } return max; } // //获取最小值 function getMin(){ var min =arguments[0]; for(var i=0;i<arguments.length;i++){ if(min>=arguments[i]){ min=arguments[i] } } return min; } //随机数 n小于m function random1Num(n,m){ return parseInt(n+Math.random()*(m-n+1)); } //打乱随机排序 function random1Number(arr){ arr.sort(function(){ return 0.5-Math.random(); }) return arr; } //返回最大值 传入的参数是数组 function getMax(arr){ var max = arr[0]; for(var i=0;i<arr.length;i++){ if(max<arr[i]){ max=arr[i] } } return max; } //返回最小值 传入的参数是数组 function getMin(arr){ var min = arr[0]; for(var i=0;i<arr.length;i++){ if(min>arr[i]){ min=arr[i] } } return min; } //返回最大值的下标 传入的是数组 function getMaxIndex(arr){ var max = arr[0]; var index; for(var i=0;i<arr.length;i++){ if(max<arr[i]){ max=arr[i] index=i; } } return index; } //返回最小下标 传入的是数组 function getMinIndex(arr){ var min = arr[arr.length-1]; var index=arr.length-1; for(var i=0;i<arr.length;i++){ if(min>arr[i]){ min=arr[i]; index=i; } } return index; } //冒泡排序 传入的是数组 function bubbling(arr){ var temp; for(var i=0;i<arr.length-1;i++){ for(var j=0;j<arr.length-1-i;j++){ if(arr[j]>arr[j+1]){ temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } return arr; } //选择排序 传入的是数组 function selectSort(arr){ var temp; for(var i=0;i<arr.length-1;i++){ for(var j=i+1;j<arr.length;j++){ if(arr[i]>arr[j]){0 temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr; } //数组判断是否有某个值 //传入的值是一个数组 n是一个字符 function has(arr,n){ for(var i in arr){ if(arr[i]===n){ return true; } } return false; } //数组去重 function norepeat(arr){ var newArr = []; for(var i in arr){ if(!has(newArr,arr[i])){ newArr.push(arr[i]) } } return newArr; } //数组插入 按照原来顺序排序 function sort1Arr(arr,n){ for(var i=0;i<arr.length;i++){ if(arr[arr.length-1]<n){ arr.push(n); }else if(arr[i]>n){ arr.splice(i,0,n); break; } } return arr } //验证码 6位数的验证码 function authCode(){ var str = ‘‘; while(str.length<6){ var num = random1Num(48,122) if((num>57&&num<65)||(num>90&&num<97)){ num = random1Num(48,122) }else{ str+=‘‘+String.fromCharCode(num) } } return str } //随机颜色 function randomColor(){ var R = random1Num(0,255) var G = random1Num(0,255) var B = random1Num(0,255) return "#"+string2Num(R,G,B) } //转换为16进制不足补0 function string2Num(r,g,b){ r = r.toString(16).length<2?"0"+r.toString(16):r.toString(16); g = g.toString(16).length<2?"0"+g.toString(16):g.toString(16); b = b.toString(16).length<2?"0"+b.toString(16):b.toString(16); return r+g+b; } //当前时间 function getdate(sign){ var date = new Date(); var time = (date.getFullYear()+"") +sign+ ((String(date.getMonth()+1)).length<2?("0"+String(date.getMonth()+1)):(String(date.getMonth()+1))) +sign+ ((String(date.getDate())).length<2?("0"+String(date.getDate())):(String(date.getDate()))) return time; } //将时间对象转换成字符串 function date2String(d,sign){ if(sign==undefined){ sign="/" } return d.getFullYear()+sign+addzero((d.getMonth()+1))+sign+addzero(d.getDate())+" "+addzero(d.getHours())+":"+addzero(d.getMinutes())+":"+addzero(d.getSeconds()); } //当数字不足两位的时候补0 应用于时间对象 function addzero(num){ var str = ""+num; return str.length<2?"0"+str:str; } //获取id元素 function $(id){ var id = id.slice(1,id.length) return document.getElementById(id) } //获取非行间样式 //ele:对象 //attr:属性 function getStyle(ele,attr){ if(ele.currentStyle){ return ele.currentStyle[attr]; }else{ return getComputedStyle(ele,false)[attr] } } //隐藏 function hide(ele){ ele.style.display="none"; } //显示 function show(ele){ ele.style.display="block"; } //获取设置自定义属性 function attr(ele,type,val){ if(arguments.length<2){return}; if(arguments.length==2){ return ele.getAttribute(type); } ele.setAttribute(type,val); } //获取到前一个兄弟节点(元素) function prevChild(ele) { return ele.previousElementSibling; } //获取当前元素距离页面的距离 function offset(ele){ var obj = {}; obj.l = ele.offsetLeft; obj.t = ele.offsetTop; while(ele.offsetParent){ var ele = ele.offsetParent; obj.l+=ele.offsetLeft; obj.t+=ele.offsetTop; } return obj; } //阻止浏览器默认行为 function prevent(e){ e.preventDefault?e.preventDefault():e.returnValue = false; } //事件监听 //第一次参数:对象 //第二个参数:事件类型 //第三个参数:回调函数 //第四个参数:是否捕获 function attach(ele,type,fn,bool){ if(!bool){ bool=false; } if(ele.addEventListener){ ele.addEventListener(type,fn,bool) }else{ ele.attachEvent("on"+type,fn) } } //阻止事件冒泡 function cancel(e){ e.stopPropagetion?e.stopPropagetion():e.cancelBubble = true; } //判断是否是闰年 function leap(year){ return (year%4==0)&&(year%100!=0||year%400==0) } //设置cookie function setCookie(_name,_val,expires){ var d = new Date(); d.setDate(d.getDate()+expires); document.cookie = _name+"="+_val+";expires="+d; } //获取cookie function getCookie(_name){ var cookie = document.cookie; var arr = cookie.split("; ") for(var i=0;i<arr.length;i++){ var newArr = arr[i].split("="); if(newArr[0]==_name){ return newArr[1]; } } } //删除cookie function removeCookie(_name,_val){ setCookie(_name,_val,-1) } //获取className 无兼容性问题 //父级元素 //class类名 function getClassName(parent,aClass){ //获取到父元素底下所有的子元素 var aEle = parent.getElementsByTagName(‘*‘); var arr = []; //匹配传进来的这个字符是不是一个边界符 var reg = new RegExp(‘\\b‘+aClass+‘\\b‘); for(var i=0;i<aEle.length;i++){ if(reg.test(aEle[i].className)){ arr.push(aEle[i]) } } return arr; } //运动框架 function move(obj,json,fn){ clearInterval(obj.timer); obj.timer = setInterval(function(){ var bStop = true; for(var attr in json){ var iCur = 0; if(attr==‘opacity‘){ iCur = parseInt(parseFloat(getStyle(obj,attr))*100) }else{ iCur = parseInt(getStyle(obj,attr)) } if(iCur!=json[attr]){ bStop=false; } var speed = (json[attr]-iCur)/8; speed = speed>0?Math.ceil(speed):Math.floor(speed); if(attr==‘opacity‘){ obj.style.opacity = (iCur+speed)/100; obj.style.filter = ‘alpha(opacity:‘+(iCur+speed)+‘)‘ }else{ obj.style[attr]=iCur+speed+‘px‘; } } if(bStop){ clearInterval(obj.timer); if(fn){ fn(); } } },30) } //获取url参数 function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象 var r = window.location.search.substr(1).match(reg); //匹配目标参数 if (r != null) return unescape(r[2]); return null; //返回参数值 } //初始化倒计时 function counttime(){ const timeCount = 60; this.count = timeCount; this.timer = setInterval(function(){ if(this.count > 0 && this.count <=timeCount){ this.count-- } else { clearInterval(this.timer) this.timer = null } },1000) } //适应移动端的viewport自适应 //<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> //<meta name="format-detection" content="telephone=no"> //uc强制竖屏 //<meta name="screen-orientation" content="portrait"> //qq强制竖屏 //<meta name="x5-orientation" content="portrait"> //uc强制全屏 //<meta name="full-screen" content="yes"> //qq强制全屏 //<meta name="x5-fullscreen" content="true"> //uc应用模式 //<meta name="browsermode" content="application"> //qq应用模式 //<meta name="x5-page-mode" content="app"> //金额千分位分割 function fenge(str){ return Number(str).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g,"$1,") } //获取前一周的日期 var date = new Date(); date.setDate(date.getDate()-7); var oneweekdate = date.getFullYear() + "-" + addzero(date.getMonth()+1) + "-" + addzero(date.getDate()) //移动端添加固定的额高度 function scrollH(rem){ var el = document.getElementById("app"); var doc = document.documentElement; el.style.height = (parseFloat(doc.clientHeight)) - (parseFloat(doc.style.fontSize)*parseFloat(rem)) + "px" } //永久本地存储 function save(key,value){ localStorage.setItem(key,JSON.stringify(value)) } //获取永久本地存储 function fecth(key){ return JSON.parse(localStorage.getItem(value)) } //ios自动聚焦 document.addEventListener(‘touchstart‘,function(e){ document.getElementById("ipt").focus(); }) //监听历史记录 pushHistory(); window.addEventListener("popstate",function(e){ window.location = "http://baibu.com"; },false); function pushHistory(){ var state = { title:‘baidu‘, url:"" }; window.history.pushState(state,"title",""); } //jq插件 jQuery.fn.extend({ text:function(value){ return access(this,function(value){ return value ===undefined? jQuery.text(this): this.empty().each(function(){ if(this.nodeType === 1 || this.nodeType ===11 || this.nodeType===9){ this.textContent = value; } }) }) } }) //金额千分位分割 function fenge(str){ str = Number(str).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g,"$1,"); return str; } //金额输入正则表达式 function formNum(obj){ obj.value = obj.value.replace(/[^0-9.]+/,"");//清除number和’.‘以外的字符 obj.value = obj.value.replace(/^\./g,"");//第一个字符是数字 obj.value = obj.value.replace(/\.{2,}/g,"");//只保留第一个‘.‘,清除多余的 obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");// obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,"$1$2.$3")//只能输入两位小数 } //判断是否是闰年 function pyear(year){ return (year%4 == 0) || (year%100!=0 && year%400==0) } //判断月份的天数 function mdays(year,mouth){ var days = 0; if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12){ days=31; }else if(mouth==6||mouth==4||mouth==9||mouth==11){ days=30; }else if(mouth==2 && ((year%4 == 0) || (year%100!=0 && year%400==0))){ days=29; }else if(mouth==2){ days=28; } return days; } //日期20180808=>2018-08-08 //vue filter.js //export const formateDate = Vue.filter(‘formateDate‘,function(val){ // var year = val.substr(0,4) // var month = val.substr(4,2) // var day = val.substr(-2) // return year + "-" + month + "-" +day // }) //卡号四位加一空格 //export const formateaccoutNo = Vue.filter(‘formateaccoutNo‘,function(val){ // var accoutNo = ‘‘; // for(var i=0;i<5;i++){ // accountNo = accoutNo + val.substr(i*4,4) + ‘‘ // } // return accoutNo // }) //金额转成大写 function converChinese(money){ //汉字的数字 var cnNums = new Array(‘零‘,‘壹‘,‘貳‘,‘叁‘,‘肆‘,‘伍‘,‘陆‘,‘柒‘,‘捌‘,‘玖‘); //基本单位 var cnIntRadice = new Array(‘‘,‘拾‘,‘佰‘,‘仟‘); //对应整数部分扩展单位 var cnIntUnits = new Array(‘‘,‘万‘,‘亿‘,‘兆‘); //对应小数部分单位 var cnDecUnits = new Array(‘角‘,‘分‘,‘毫‘,‘厘‘); //整数金额时后面跟的单位 var cnInteger = "整"; //整型后的单位 var cnIntLast = ‘元‘; //最大处理的数字 var maxNum = 9999999999999999.9999; //金额整数部分 var integerNum; //金额小数部分 var decimalNum; //输出的中文金额字符串 var chineseStr = ‘‘; //分离金额后用的数组,预定义 var parts; if(money == ‘‘){ return ‘‘; } money = parseFloat(money) //超出最大处理数字 if(money >= maxNum){ return ‘‘ } if(money == 0){ chineseStr = cnNums[0] + cnIntLast + cnInteger; return chineseStr; } //转换成字符串 money = money.toString(); if(money.indexOf(‘.‘) == -1){ integerNum = money; decimalNum =‘‘; } else { parts = money.split(‘.‘); integerNum = parts[0]; decimalNum = parts[1].substr(0,4); } //获取整型部分转换 if(parseInt(integerNum,10) > 0){ var zeroCount = 0; var IntLen = integerNum.length; for(var i = 0;i < IntLen; i++){ var n = integerNum.substr(i,1); var p = IntLen - i - 1; var q = p/4; var m = p%4; if(n === ‘0‘){ zeroCount++; } else { if(zeroCount > 0){ chineseStr += cnNums[0]; } //归零 zeroCount = 0; chineseStr += cnNums[parseInt(n)] + cnIntRadice[m]; } if(m == 0 && zeroCount < 4){ chineseStr += cnIntUnits[q]; } } chineseStr += cnIntLast; } //小数部分 if(decimalNum != ‘‘){ var decLen = decimalNum.length; for(var i=0;i<decLen;i++){ var n = decimalNum.substr(i,1); if(n !=‘0‘){ chineseStr += cnNums[Number(n)] + cnDecUnits[i] } } } if(chineseStr == ‘‘){ chineseStr += cnNums[0] + cnIntLast + cnInteger; }else if(decimalNum == ‘‘){ chineseStr += cnInteger; } return chineseStr; } //初始化短信倒计时 function getCodel(){ const timeCount = 60; var count = timeCount; var self = this; this.timer = setInterval(function(){ if(self.count > 0 && self.count <= timeCount){ self.count --; } else { self.show = true; clearInterval(self.timer) self.timer = null } },1000) } //首字母大写过滤器 // filters: { // capitalize: function (value) { // if (!value) return ‘‘ // value = value.toString() // return value.charAt(0).toUpperCase() + value.slice(1) // } //ie9 placeholder模拟 function ifie9(){ //判断是否是ie9 if(navigator.userAgent.indexOf("MSIE 9.0")>0){ //input元素 $(".ipt").each(function(){ var self = $(this); self.parent().append(‘<span class="placeholder" data-type = "placeholder"></span>‘) if(self.val() != ‘‘){ self.parent().find("span.placeholder").hide() }else{ self.parent().find("span.placeholder").show() } }).on(‘foucs‘,function(){ $(this).parent().find("span.placeholder").hide() }).on(‘blur‘,function(){ var self = $(this); if(self.val != ‘‘){ self.parent().find("span.placeholder").hide() }else{ self.parent().find("span.placeholder").show() } }); $("span.placeholder").on(‘clock‘,function(){ $(this).hide() }) } } //滚动条 $(document).ready(function() { $(window).scroll(function() { //$(document).scrollTop() 获取垂直滚动的距离 //$(document).scrollLeft() 这是获取水平滚动条的距离 if ($(document).scrollTop() <= 0) { alert("滚动条已经到达顶部为0"); } if ($(document).scrollTop() >= $(document).height() - $(window).height()) { alert("滚动条已经到达底部为" + $(document).scrollTop()); } }); }); //有就删除,没有就添加 $(".bo").toggleClass(‘box‘) //保留两位小数,非四舍五入 function cuntnum(num){ if(num.toString().indexOf(".") > -1){ var c = num.toString().indexOf(".") var d = num.toString().substr(c+1,2) var e = num.toString().substring(0,c+1) return e+d }else if(num.toString().indexOf(".") == -1){ return num } }
//import { setInterval } from "timers";
//获取最大值
function getMax(){
var max = arguments[0];
for(var i=0;i<arguments.length;i++){
if(max<=arguments[i]){
max=arguments[i]
}
}
return max;
}
//
//获取最小值
function getMin(){
var min =arguments[0];
for(var i=0;i<arguments.length;i++){
if(min>=arguments[i]){
min=arguments[i]
}
}
return min;
}
//随机数 n小于m
function random1Num(n,m){
return parseInt(n+Math.random()*(m-n+1));
}
//打乱随机排序
function random1Number(arr){
arr.sort(function(){
return 0.5-Math.random();
})
return arr;
}
//返回最大值 传入的参数是数组
function getMax(arr){
var max = arr[0];
for(var i=0;i<arr.length;i++){
if(max<arr[i]){
max=arr[i]
}
}
return max;
}
//返回最小值 传入的参数是数组
function getMin(arr){
var min = arr[0];
for(var i=0;i<arr.length;i++){
if(min>arr[i]){
min=arr[i]
}
}
return min;
}
//返回最大值的下标 传入的是数组
function getMaxIndex(arr){
var max = arr[0];
var index;
for(var i=0;i<arr.length;i++){
if(max<arr[i]){
max=arr[i]
index=i;
}
}
return index;
}
//返回最小下标 传入的是数组
function getMinIndex(arr){
var min = arr[arr.length-1];
var index=arr.length-1;
for(var i=0;i<arr.length;i++){
if(min>arr[i]){
min=arr[i];
index=i;
}
}
return index;
}
//冒泡排序 传入的是数组
function bubbling(arr){
var temp;
for(var i=0;i<arr.length-1;i++){
for(var j=0;j<arr.length-1-i;j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
return arr;
}
//选择排序 传入的是数组
function selectSort(arr){
var temp;
for(var i=0;i<arr.length-1;i++){
for(var j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){0
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
//数组判断是否有某个值
//传入的值是一个数组 n是一个字符
function has(arr,n){
for(var i in arr){
if(arr[i]===n){
return true;
}
}
return false;
}
//数组去重
function norepeat(arr){
var newArr = [];
for(var i in arr){
if(!has(newArr,arr[i])){
newArr.push(arr[i])
}
}
return newArr;
}
//数组插入 按照原来顺序排序
function sort1Arr(arr,n){
for(var i=0;i<arr.length;i++){
if(arr[arr.length-1]<n){
arr.push(n);
}else if(arr[i]>n){
arr.splice(i,0,n);
break;
}
}
return arr
}
//验证码 6位数的验证码
function authCode(){
var str = ‘‘;
while(str.length<6){
var num = random1Num(48,122)
if((num>57&&num<65)||(num>90&&num<97)){
num = random1Num(48,122)
}else{
str+=‘‘+String.fromCharCode(num)
}
}
return str
}
//随机颜色
function randomColor(){
var R = random1Num(0,255)
var G = random1Num(0,255)
var B = random1Num(0,255)
return "#"+string2Num(R,G,B)
}
//转换为16进制不足补0
function string2Num(r,g,b){
r = r.toString(16).length<2?"0"+r.toString(16):r.toString(16);
g = g.toString(16).length<2?"0"+g.toString(16):g.toString(16);
b = b.toString(16).length<2?"0"+b.toString(16):b.toString(16);
return r+g+b;
}
//当前时间
function getdate(sign){
var date = new Date();
var time = (date.getFullYear()+"") +sign+ ((String(date.getMonth()+1)).length<2?("0"+String(date.getMonth()+1)):(String(date.getMonth()+1))) +sign+ ((String(date.getDate())).length<2?("0"+String(date.getDate())):(String(date.getDate())))
return time;
}
//将时间对象转换成字符串
function date2String(d,sign){
if(sign==undefined){
sign="/"
}
return d.getFullYear()+sign+addzero((d.getMonth()+1))+sign+addzero(d.getDate())+" "+addzero(d.getHours())+":"+addzero(d.getMinutes())+":"+addzero(d.getSeconds());
}
//当数字不足两位的时候补0 应用于时间对象
function addzero(num){
var str = ""+num;
return str.length<2?"0"+str:str;
}
//获取id元素
function $(id){
var id = id.slice(1,id.length)
return document.getElementById(id)
}
//获取非行间样式
//ele:对象
//attr:属性
function getStyle(ele,attr){
if(ele.currentStyle){
return ele.currentStyle[attr];
}else{
return getComputedStyle(ele,false)[attr]
}
}
//隐藏
function hide(ele){
ele.style.display="none";
}
//显示
function show(ele){
ele.style.display="block";
}
//获取设置自定义属性
function attr(ele,type,val){
if(arguments.length<2){return};
if(arguments.length==2){
return ele.getAttribute(type);
}
ele.setAttribute(type,val);
}
//获取到前一个兄弟节点(元素)
function prevChild(ele) {
return ele.previousElementSibling;
}
//获取当前元素距离页面的距离
function offset(ele){
var obj = {};
obj.l = ele.offsetLeft;
obj.t = ele.offsetTop;
while(ele.offsetParent){
var ele = ele.offsetParent;
obj.l+=ele.offsetLeft;
obj.t+=ele.offsetTop;
}
return obj;
}
//阻止浏览器默认行为
function prevent(e){
e.preventDefault?e.preventDefault():e.returnValue = false;
}
//事件监听
//第一次参数:对象
//第二个参数:事件类型
//第三个参数:回调函数
//第四个参数:是否捕获
function attach(ele,type,fn,bool){
if(!bool){
bool=false;
}
if(ele.addEventListener){
ele.addEventListener(type,fn,bool)
}else{
ele.attachEvent("on"+type,fn)
}
}
//阻止事件冒泡
function cancel(e){
e.stopPropagetion?e.stopPropagetion():e.cancelBubble = true;
}
//判断是否是闰年
function leap(year){
return (year%4==0)&&(year%100!=0||year%400==0)
}
//设置cookie
function setCookie(_name,_val,expires){
var d = new Date();
d.setDate(d.getDate()+expires);
document.cookie = _name+"="+_val+";expires="+d;
}
//获取cookie
function getCookie(_name){
var cookie = document.cookie;
var arr = cookie.split("; ")
for(var i=0;i<arr.length;i++){
var newArr = arr[i].split("=");
if(newArr[0]==_name){
return newArr[1];
}
}
}
//删除cookie
function removeCookie(_name,_val){
setCookie(_name,_val,-1)
}
//获取className 无兼容性问题
//父级元素
//class类名
function getClassName(parent,aClass){
//获取到父元素底下所有的子元素
var aEle = parent.getElementsByTagName(‘*‘);
var arr = [];
//匹配传进来的这个字符是不是一个边界符
var reg = new RegExp(‘\\b‘+aClass+‘\\b‘);
for(var i=0;i<aEle.length;i++){
if(reg.test(aEle[i].className)){
arr.push(aEle[i])
}
}
return arr;
}
//运动框架
function move(obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var bStop = true;
for(var attr in json){
var iCur = 0;
if(attr==‘opacity‘){
iCur = parseInt(parseFloat(getStyle(obj,attr))*100)
}else{
iCur = parseInt(getStyle(obj,attr))
}
if(iCur!=json[attr]){
bStop=false;
}
var speed = (json[attr]-iCur)/8;
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(attr==‘opacity‘){
obj.style.opacity = (iCur+speed)/100;
obj.style.filter = ‘alpha(opacity:‘+(iCur+speed)+‘)‘
}else{
obj.style[attr]=iCur+speed+‘px‘;
}
}
if(bStop){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30)
}
//获取url参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]); return null; //返回参数值
}
//初始化倒计时
function counttime(){
const timeCount = 60;
this.count = timeCount;
this.timer = setInterval(function(){
if(this.count > 0 && this.count <=timeCount){
this.count--
} else {
clearInterval(this.timer)
this.timer = null
}
},1000)
}
//适应移动端的viewport自适应
//<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
//<meta name="format-detection" content="telephone=no">
//uc强制竖屏
//<meta name="screen-orientation" content="portrait">
//qq强制竖屏
//<meta name="x5-orientation" content="portrait">
//uc强制全屏
//<meta name="full-screen" content="yes">
//qq强制全屏
//<meta name="x5-fullscreen" content="true">
//uc应用模式
//<meta name="browsermode" content="application">
//qq应用模式
//<meta name="x5-page-mode" content="app">
//金额千分位分割
function fenge(str){
return Number(str).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g,"$1,")
}
//获取前一周的日期
var date = new Date();
date.setDate(date.getDate()-7);
var oneweekdate = date.getFullYear() + "-" + addzero(date.getMonth()+1) + "-" + addzero(date.getDate())
//移动端添加固定的额高度
function scrollH(rem){
var el = document.getElementById("app");
var doc = document.documentElement;
el.style.height = (parseFloat(doc.clientHeight)) - (parseFloat(doc.style.fontSize)*parseFloat(rem)) + "px"
}
//永久本地存储
function save(key,value){
localStorage.setItem(key,JSON.stringify(value))
}
//获取永久本地存储
function fecth(key){
return JSON.parse(localStorage.getItem(value))
}
//ios自动聚焦
document.addEventListener(‘touchstart‘,function(e){
document.getElementById("ipt").focus();
})
//监听历史记录
pushHistory();
window.addEventListener("popstate",function(e){
window.location = "http://baibu.com";
},false);
function pushHistory(){
var state = {
title:‘baidu‘,
url:""
};
window.history.pushState(state,"title","");
}
//jq插件
jQuery.fn.extend({
text:function(value){
return access(this,function(value){
return value ===undefined?
jQuery.text(this):
this.empty().each(function(){
if(this.nodeType === 1 || this.nodeType ===11 || this.nodeType===9){
this.textContent = value;
}
})
})
}
})
//金额千分位分割
function fenge(str){
str = Number(str).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g,"$1,");
return str;
}
//金额输入正则表达式
function formNum(obj){
obj.value = obj.value.replace(/[^0-9.]+/,"");//清除number和’.‘以外的字符
obj.value = obj.value.replace(/^\./g,"");//第一个字符是数字
obj.value = obj.value.replace(/\.{2,}/g,"");//只保留第一个‘.‘,清除多余的
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,"$1$2.$3")//只能输入两位小数
}
//判断是否是闰年
function pyear(year){
return (year%4 == 0) || (year%100!=0 && year%400==0)
}
//判断月份的天数
function mdays(year,mouth){
var days = 0;
if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12){
days=31;
}else if(mouth==6||mouth==4||mouth==9||mouth==11){
days=30;
}else if(mouth==2 && ((year%4 == 0) || (year%100!=0 && year%400==0))){
days=29;
}else if(mouth==2){
days=28;
}
return days;
}
//日期20180808=>2018-08-08
//vue filter.js
//export const formateDate = Vue.filter(‘formateDate‘,function(val){
// var year = val.substr(0,4)
// var month = val.substr(4,2)
// var day = val.substr(-2)
// return year + "-" + month + "-" +day
// })
//卡号四位加一空格
//export const formateaccoutNo = Vue.filter(‘formateaccoutNo‘,function(val){
// var accoutNo = ‘‘;
// for(var i=0;i<5;i++){
// accountNo = accoutNo + val.substr(i*4,4) + ‘‘
// }
// return accoutNo
// })
//金额转成大写
function converChinese(money){
//汉字的数字
var cnNums = new Array(‘零‘,‘壹‘,‘貳‘,‘叁‘,‘肆‘,‘伍‘,‘陆‘,‘柒‘,‘捌‘,‘玖‘);
//基本单位
var cnIntRadice = new Array(‘‘,‘拾‘,‘佰‘,‘仟‘);
//对应整数部分扩展单位
var cnIntUnits = new Array(‘‘,‘万‘,‘亿‘,‘兆‘);
//对应小数部分单位
var cnDecUnits = new Array(‘角‘,‘分‘,‘毫‘,‘厘‘);
//整数金额时后面跟的单位
var cnInteger = "整";
//整型后的单位
var cnIntLast = ‘元‘;
//最大处理的数字
var maxNum = 9999999999999999.9999;
//金额整数部分
var integerNum;
//金额小数部分
var decimalNum;
//输出的中文金额字符串
var chineseStr = ‘‘;
//分离金额后用的数组,预定义
var parts;
if(money == ‘‘){
return ‘‘;
}
money = parseFloat(money)
//超出最大处理数字
if(money >= maxNum){
return ‘‘
}
if(money == 0){
chineseStr = cnNums[0] + cnIntLast + cnInteger;
return chineseStr;
}
//转换成字符串
money = money.toString();
if(money.indexOf(‘.‘) == -1){
integerNum = money;
decimalNum =‘‘;
} else {
parts = money.split(‘.‘);
integerNum = parts[0];
decimalNum = parts[1].substr(0,4);
}
//获取整型部分转换
if(parseInt(integerNum,10) > 0){
var zeroCount = 0;
var IntLen = integerNum.length;
for(var i = 0;i < IntLen; i++){
var n = integerNum.substr(i,1);
var p = IntLen - i - 1;
var q = p/4;
var m = p%4;
if(n === ‘0‘){
zeroCount++;
} else {
if(zeroCount > 0){
chineseStr += cnNums[0];
}
//归零
zeroCount = 0;
chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if(m == 0 && zeroCount < 4){
chineseStr += cnIntUnits[q];
}
}
chineseStr += cnIntLast;
}
//小数部分
if(decimalNum != ‘‘){
var decLen = decimalNum.length;
for(var i=0;i<decLen;i++){
var n = decimalNum.substr(i,1);
if(n !=‘0‘){
chineseStr += cnNums[Number(n)] + cnDecUnits[i]
}
}
}
if(chineseStr == ‘‘){
chineseStr += cnNums[0] + cnIntLast + cnInteger;
}else if(decimalNum == ‘‘){
chineseStr += cnInteger;
}
return chineseStr;
}
//初始化短信倒计时
function getCodel(){
const timeCount = 60;
var count = timeCount;
var self = this;
this.timer = setInterval(function(){
if(self.count > 0 && self.count <= timeCount){
self.count --;
} else {
self.show = true;
clearInterval(self.timer)
self.timer = null
}
},1000)
}
//首字母大写过滤器
// filters: {
// capitalize: function (value) {
// if (!value) return ‘‘
// value = value.toString()
// return value.charAt(0).toUpperCase() + value.slice(1)
// }
//ie9 placeholder模拟
function ifie9(){
//判断是否是ie9
if(navigator.userAgent.indexOf("MSIE 9.0")>0){
//input元素
$(".ipt").each(function(){
var self = $(this);
self.parent().append(‘<span class="placeholder" data-type = "placeholder"></span>‘)
if(self.val() != ‘‘){
self.parent().find("span.placeholder").hide()
}else{
self.parent().find("span.placeholder").show()
}
}).on(‘foucs‘,function(){
$(this).parent().find("span.placeholder").hide()
}).on(‘blur‘,function(){
var self = $(this);
if(self.val != ‘‘){
self.parent().find("span.placeholder").hide()
}else{
self.parent().find("span.placeholder").show()
}
});
$("span.placeholder").on(‘clock‘,function(){
$(this).hide()
})
}
}
//滚动条
$(document).ready(function() {
$(window).scroll(function() {
//$(document).scrollTop() 获取垂直滚动的距离
//$(document).scrollLeft() 这是获取水平滚动条的距离
if ($(document).scrollTop() <= 0) {
alert("滚动条已经到达顶部为0");
}
if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
alert("滚动条已经到达底部为" + $(document).scrollTop());
}
});
});
//有就删除,没有就添加
$(".bo").toggleClass(‘box‘)
//保留两位小数,非四舍五入
function cuntnum(num){
if(num.toString().indexOf(".") > -1){
var c = num.toString().indexOf(".")
var d = num.toString().substr(c+1,2)
var e = num.toString().substring(0,c+1)
return e+d
}else if(num.toString().indexOf(".") == -1){
return num
}
}
以上是关于常用的功能封装 pool.js的主要内容,如果未能解决你的问题,请参考以下文章