移动端REM
(function() {
function w() {
var r = document.documentElement;
var a = r.getBoundingClientRect().width;
if(a > 750) {
a = 750;
}
rem = a / 7.5;
r.style.fontSize = rem + "px"
}
var t;
w();
window.addEventListener("resize", function() {
clearTimeout(t);
t = setTimeout(w, 300)
}, false);
})();
移除浏览器默认事件(移动端)
document.addEventListener('touchstart',function(){
return false;
},true);
mobile还是PC
function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
console.log("mobile")
} else {
console.log("pc")
}
}
页面切换title改变
window.addEventListener("visibilitychange", function(event) {
if(document.hidden){
document.title = 200;
}else{
document.title = 100;
}
});
全屏
// 兼容IE低版本
function iefull(){
var el = document.documentElement;
var rfs = el.msRequestFullScreen;
if(typeof window.ActiveXObject != "undefined") {
var wscript = new ActiveXObject("WScript.Shell");
if(wscript != null) {
wscript.SendKeys("{F11}");
}
}
}
function toggleFullScreen() {
if (!document.fullscreenElement &&
!document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
if (document.documentElement.requestFullscreen) {
document.documentElement.requestFullscreen();
} else if (document.documentElement.msRequestFullscreen) {
document.documentElement.msRequestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
document.documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
}
获取年月日时分秒
function times(arr,type){
if(!type){
var year = new Date(arr).getFullYear();
var month = new Date(arr).getMonth()+1 >= 10 ? new Date(arr).getMonth()+1 : "0" + (new Date(arr).getMonth()+1);
var day = Number(new Date(arr).getDate()) >= 10 ? Number(new Date(arr).getDate()) : "0" + Number(new Date(arr).getDate());
var hours = new Date(arr).getHours() >= 10 ? new Date(arr).getHours() : "0" + new Date(arr).getHours();
var minutes = new Date(arr).getMinutes() >= 10 ? new Date(arr).getMinutes() : "0" + new Date(arr).getMinutes();
var seconds = new Date(arr).getSeconds() >= 10 ? new Date(arr).getSeconds() : "0" + new Date(arr).getSeconds();
var Time = year + "-" + month + "-" + day
var days = hours+":"+minutes+":"+seconds
var obj = [Time,days]
return obj
}else{
arr.filter((item)=>{
var year = new Date(item[type]).getFullYear();
var month = new Date(item[type]).getMonth()+1 >= 10 ? new Date(item[type]).getMonth()+1 : "0" + (new Date(item[type]).getMonth()+1);
var day = new Date(item[type]).getDate()>= 10 ? new Date(item[type]).getDate() : "0" + (new Date(item[type]).getDate());
var hours = new Date(item[type]).getHours() >= 10 ? new Date(item[type]).getHours() : "0" + new Date(item[type]).getHours();
var minutes = new Date(item[type]).getMinutes() >= 10 ? new Date(item[type]).getMinutes() : "0" + new Date(item[type]).getMinutes();
var seconds = new Date(item[type]).getSeconds() >= 10 ? new Date(item[type]).getSeconds() : "0" + new Date(item[type]).getSeconds();
item.Time = year + "-" + month + "-" + day
item.days = hours+":"+minutes+":"+seconds
})
return arr;
}
}
取出随机字符串
function randChar(length,characters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"){
if(length instanceof Number){
characters=characters.split("");
result="";
while(result.length<length) result+=characters[Math.round(Math.random()*characters.length)];
return result;
}else{
return alert("请输入数字")
}
}
取出随机字色
function t_RandomColor(c){
var str = "#";
var arr = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
if(c == undefined){
for(var i =0; i<6;i++){
str += arr[Math.ceil(Math.random()*15)]
}
return str
}else{
var error = typeof c !== 'number' ? "请输入数字" : c < 0 ? "此方法支持0-1!0.5包括0.5为淡色,反之为重色" : c > 1 ? "此方法支持0-1!0.5包括0.5为淡色,反之为重色" : true
if(error == true){
if(c<0.6){
for(var i =0; i<6;i++){
str += arr[Math.ceil(Math.random()*6+9)]
}
}else{
for(var i =0; i<6;i++){
str += arr[Math.ceil(Math.random()*6)]
}
}
return str
}else{
alert(error)
}
}
}
对象转换为URL拼接
function Url(obj,arr){
if (obj instanceof Object && obj instanceof Array == false && arr instanceof Array ) {
var ary = [];
var obr = {};
var str = "";
Object.keys(obj).forEach(function(key){
ary.push(obj[key])
});
for(let i =0,sum = arr.length; i < sum; i++){
obr[arr[i]] = ary[i]
}
if(ary.length != arr.length){
return alert("参数长度不一致!");
}else{
Object.keys(obr).forEach(function(key){
str += key +"="+obr[key]+"&"
});
return str.slice(0,str.length-1)
}
}else{
return alert("参数一为对象,二为数组");
}
}
取出你想要的键值
function add(obj,arr){
var o = {}
arr.filter((item)=>{
if(obj[item]) o[item] = obj[item] ;
})
return o
}
日常正则
一、替换所有标签
function matchReg(str){
let reg=/<\/?.+?\/?>/g;
return str.replace(reg,'')
}
二、校验是否全由11位数字组成
function matchReg(str){
let reg=/<\/?.+?\/?>/g;
return str.replace(reg,'')
}
三、是否带有小数
function matchReg(str){
let reg=/<\/?.+?\/?>/g;
return str.replace(reg,'')
}
四、校验是否中文名称组成
function t_IsChina(str) {
var reg=/^[\u4E00-\u9FA5]{2,4}$/;
return reg.test(str);
}
五、校验邮件地址是否合法
function t_IsEmail(str) {
var reg=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
return reg.test(str);
}
六、校验身份证
function userCar(str) {
var reg=/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
return reg.test(str);
}
七、邮政编码
function dawkcode(str) {
var reg=/[1-9]\d{5}(?!\d)/;
return reg.test(str);
}