// promise封装jquery的ajax请求
// 请求例子 Ajax({url:"/api",type:“get”,data:""}).then((res)=>{ console.log(res) })
function Ajax (param) {
return new Promise(function(resolve, reject) {
$.ajax({
url: param.url,
type: param.type,
data: param.data || ‘’,
dataType: “json”,
success: function(data) {
resolve(data);
},
error: function(error) {
reject(error)
}
});
});
};
function addCookie(e, t, a) {
var n = e + “=” + escape(t) + “; path=/”;
if(a > 0) {
var r = new Date;
r.setTime(r.getTime() + a * 3600 * 1e3);
n = n + “;expires=” + r.toGMTString()
}
document.cookie = n
}
// 设置cookie
function setCookie(name, value, days) {
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
var arr = document.cookie.match(new RegExp("(^| )" + name + “=([^;]*)(;|$)”));
document.cookie = name + “=” + escape(value) + “;expires=” + exp.toGMTString();
}
// 获取cookie
function getCookie(e) {
var t = document.cookie;
var a = t.split("; “);
for(var n = 0; n < a.length; n++) {
var r = a[n].split(”=");
if(r[0] == e) return unescape(r[1])
}
return null
}
// 删除cookie
function delCookie(e) {
var t = new Date;
t.setTime(t.getTime() - 1);
var a = getCookie(e);
if(a != null) document.cookie = e + “=” + a + “; path=/;expires=” + t.toGMTString()
}
// 常用运动函数
function animate(ele, attr_json, callback) {
// 清除定时器,避免动画重合
clearInterval(ele.timer);
ele.timer = setInterval(function() {
var flag = true; //定时器是否清除的标记值
for(var attr in attr_json) {
var current = 0;
//获取当前样式
if(attr == "opacity") { //如果是透明度,那么返回值,如果不兼容就返回0
current = Math.round(parseInt(getStyle(ele, attr) * 100)) || 0;
} else { //其他
current = parseInt(getStyle(ele, attr));
console.log(current)
}
//计算步长,并进行取整来避免除不尽引起的误差
var stepLength = (attr_json[attr] - current) / 10;
stepLength = stepLength > 0 ? Math.ceil(stepLength) : Math.floor(stepLength);
//判断要改变的样式是否是透明度
if(attr == "opacity") {
if("opacity" in ele.style) {
ele.style.opacity = (current + stepLength) / 100;
} else {
ele.style.filter = "alpha(opacity = " + (current + stepLength) * 10 + ")";
}
}
//判断要改变的样式是否是层级
else if(attr == "zIndex") {
ele.style.zIndex = current + stepLength;
}
//其他属性
else {
ele.style[attr] = (current + stepLength) + "px";
}
//判断是否清除定时器
if(current != attr_json[attr]) {
flag = false;
}
}
if(flag) {
clearInterval(ele.timer);
if(callback) {
callback();
}
}
}, 10)
}
// 获取元素某个css属性
function getStyle(ele, attr) {
if(window.getComputedStyle) { //兼容性检测,优先采用W3C标准
if (window.getComputedStyle(ele, null)[attr]!==undefined) {
return window.getComputedStyle(ele, null)[attr];
}
return ele[attr]
} else {
return ele.currentStyle[attr]; //兼容Ie低版本浏览器
}
}
// 时间戳转换时间
function convertDate (number){
number = parseInt(number);
return new Date(number * 1000).toLocalTimeString();
}
// 获取url参数
function getUrlParm (url) {
url = url ? url : window.location.href;
let _pa = url.substring(url.indexOf(’?’) + 1), _arrS = _pa.split(’&’), _rs = {};
for (var i = 0, _len = _arrS.length; i < _len; i++) {
let pos = _arrS[i].indexOf(’=’);
if (pos == -1) {
continue;
}
let name = _arrS[i].substring(0, pos), value = window.decodeURIComponent(_arrS[i].substring(pos + 1));
_rs[name] = value;
}
return _rs;
}