常用js整理
//获取Url中的参数值
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return r[2];
}
else {
return "";
}
}
//相当于锚点
$("html,body").animate({scrollTop: $(".top").offset().top}, 1000);
var sec = document.getElementById("second");
var i = 5;
var timer = setInterval(function() {
i--;
sec.innerHTML = i;
if (i == 1) {
window.location.href = "";
}
},
1000);
//indexOf判断数组中是否包含某个元素(兼容)
if (!Array.indexOf) {
Array.prototype.indexOf = function(el) {
for (var i = 0,
n = this.length; i < n; i++) {
if (this[i] === el) {
return i;
}
}
return - 1;
}
}
var arry = ["xml", "html", "css", "js"];
console.log(arry.indexOf('js'))//js判断ie浏览器版本
var isIE7 = !!navigator.userAgent.match(/MSIE 7.0/);
var isIE8 = !!navigator.userAgent.match(/MSIE 8.0/);
var isIE9 = !!navigator.userAgent.match(/MSIE 9.0/);
//javascript取浏览器窗口宽(兼容)相当于$(window).width()
var width= document.documentElement.clientWidth || document.body.clientWidth;
//传入的float数字 ,n:希望返回小数点几位
function formactNum(s, n) {
n = n > 0 && n <= 20 ? n: 2;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
var l = s.split(".")[0].split("").reverse(),
r = s.split(".")[1];
t = "";
for (i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ",": "");
}
return t.split("").reverse().join("") + "." + r;
}//判断浏览器是否支持css属于 也可以判断ie浏览器版本
if ('borderRadius' in document.createElement('div').style) {
//支持圆角为ie9或以上
}
if ('placeholder' in document.createElement('input') {
//如果input支持placeholder 得知ie版本为9以上
}//javascript添加删除样式类名
function hasClass(elements, cName) {
return !! elements.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
};
function addClass(elements, cName) {
if (!hasClass(elements, cName)) {
if (elements.className) {
elements.className += " " + cName;
} else {
elements.className += cName;
}
};
};
function removeClass(elements, cName) {
if (hasClass(elements, cName)) {
elements.className = elements.className.replace(new RegExp("(\\s|^)" + cName + "(\\s|$)"), "");
};
};
//移动端触摸
$('.scroll ul').on('touchstart',
function(e) {
var _touch = e.originalEvent.targetTouches[0];
var str = $(this)[0].style.transform,
ely = 0;
if (str != "") {
var translate = str.substring(str.indexOf("(") + 1, str.lastIndexOf(","));
ely = translate.split(',')[1];
}
_y = _touch.pageY - parseFloat(ely);
e.preventDefault();
});