js通用库(依赖JQ)
var publicObj = {
init:function () {
//this.dataStatistics();
this.makeLoadDom();
this.makeAlertDom();
},
//数据统计
dataStatistics:function () {
var dsStr ='<script type=\'text/javascript\'>\n' +
' var _vds = _vds || [];\n' +
' window._vds = _vds;\n' +
' (function(){\n' +
' _vds.push([\'setAccountId\', \'aa3c1591e205312c\']);\n' +
' (function() {\n' +
' var vds = document.createElement(\'script\');\n' +
' vds.type=\'text/javascript\';\n' +
' vds.async = true;\n' +
' vds.src = (\'https:\' == document.location.protocol ? \'https://\' : \'http://\') + \'assets.growingio.com/vds.js\';\n' +
' var s = document.getElementsByTagName(\'script\')[0];\n' +
' s.parentNode.insertBefore(vds, s);\n' +
' })();\n' +
' })();\n' +
''
$("body").append(dsStr);
},
//倒计时
getActiveEndTime:function ( endTimeStr) {
var interval =setInterval(function () {
var str ='';
var endTime = endTimeStr;
var date =new Date();
var nowTime = date.getTime();
endTime =parseInt(endTime /1000);
var countdownTime = endTime -parseInt(nowTime /1000);
var day;
var hour;
var min;
var sec;
var allhour;
day =parseInt(countdownTime / (3600 *24));
hour =parseInt((countdownTime - day * (3600 *24)) /3600);
allhour =parseInt(countdownTime /3600);
min =parseInt((countdownTime - day * (3600 *24) - hour *3600) /60);
sec = countdownTime - day * (3600 *24) - hour *3600 - min *60;
if (sec <=0 && min <=0 && hour <=0) {
sec ="00";
min ="00";
hour ="00";
allhour ="00";
day ="00";
}else {
if (Number(sec) <10) {
sec ="0" + sec;
}
if (Number(min) <10) {
min ="0" + min;
}
if (Number(hour) <10) {
hour ="0" + hour;
}
if (Number(allhour) <10) {
allhour ="0" + allhour;
}
if (Number(day) <10) {
day ="0" + day;
}
}
var cd2Str =' <div class="cdnum">'+hour+'</div>\n' +
' <div class="cdunit">:</div>\n' +
' <div class="cdnum">'+min+'</div>\n' +
' <div class="cdunit">:</div>\n' +
' <div class="cdnum">'+sec+'</div>\n' +
'
'$(".coutdownbox").html(cd2Str)
}, 1000);
},
countdown:function (countdownStr,sBtn,hBtn,count) {
$(sBtn).hide()
$(hBtn).html(count+"s")
$(hBtn).show()
countdownStr =setInterval(function () {
count--;
if (count >=1) {
$(hBtn).html(count+"s")
}else {
clearInterval(countdownStr);
$(hBtn).hide()
$(sBtn).show()
}
}, 1000)
},
//加载动画
makeLoadDom:function () {
var loadStr =' <div class="loadbox">\n' +
' <div class="spinner">\n' +
' <div class="rect1"></div>\n' +
' <div class="rect2"></div>\n' +
' <div class="rect3"></div>\n' +
' <div class="rect4"></div>\n' +
' <div class="rect5"></div>\n' +
' </div>\n' +
' </div>';
$("body").append(loadStr);
},
showLoad:function () {
$(".loadbox").show();
},
hideLoad:function () {
$(".loadbox").hide();
},
//获得链接参数
getUrlData:function (name) {
var reg =new RegExp("(^|&)" + name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r !=null)return unescape(r[2]);
return null;
},
//提示弹框
makeAlertDom:function () {
var domStr ='<div class="myalert" >\n' +
' <div class="layer"></div>\n' +
' <div class="tip"></div>\n' +
' '
$("body").append(domStr);
},
myalert:function (tip) {
$(".myalert > .tip").text(tip);
$(".myalert").show()
var int;
clearTimeout(int)
int =setTimeout(function () {
$(".myalert").hide();
$(".myalert > .tip").text('');
}, 1000);
},
//加减乘除
plusNum:function (arg1, arg2) {
var r1, r2, m, c;
try {
r1 = arg1.toString().split(".")[1].length;
}
catch (e) {
r1 =0;
}
try {
r2 = arg2.toString().split(".")[1].length;
}
catch (e) {
r2 =0;
}
c = Math.abs(r1 - r2);
m = Math.pow(10, Math.max(r1, r2));
if (c >0) {
var cm = Math.pow(10, c);
if (r1 > r2) {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", "")) * cm;
}else {
arg1 = Number(arg1.toString().replace(".", "")) * cm;
arg2 = Number(arg2.toString().replace(".", ""));
}
}else {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", ""));
}
return (arg1 + arg2) / m;
},
reduceNum:function (arg1, arg2) {
var r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length;
}
catch (e) {
r1 =0;
}
try {
r2 = arg2.toString().split(".")[1].length;
}
catch (e) {
r2 =0;
}
m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
return Number(((arg1 * m - arg2 * m) / m).toFixed(n));
},
accMul:function (arg1, arg2) {
var m =0, s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split(".")[1].length
}catch (e) {
}
try {
m += s2.split(".")[1].length
}catch (e) {
}
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
},
except:function (arg1, arg2) {
var t1 =0, t2 =0, r1, r2;
try {
t1 = arg1.toString().split(".")[1].length;
}
catch (e) {
}
try {
t2 = arg2.toString().split(".")[1].length;
}
catch (e) {
}
with (Math) {
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return (r1 / r2) *pow(10, t2 - t1);
}
},
//是否为中文
isChinese:function (temp) {
var re =/^[\u4E00-\u9FA5]+$/;
if (re.test(temp)){
return true;
}else {
return false;
}
},
}
publicObj.init();