class StorageFn { constructor () { this.ls = window.localStorage; this.ss = window.sessionStorage; } /*-----------------cookie---------------------*/ /*设置cookie*/ setCookie (name, value, day) { var setting = arguments[0]; if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){ for (var i in setting) { var oDate = new Date(); oDate.setDate(oDate.getDate() + day); document.cookie = i + '=' + setting[i] + ';expires=' + oDate; } }else{ var oDate = new Date(); oDate.setDate(oDate.getDate() + day); document.cookie = name + '=' + value + ';expires=' + oDate; } } /*获取cookie*/ getCookie (name) { var arr = document.cookie.split('; '); for (var i = 0; i < arr.length; i++) { var arr2 = arr[i].split('='); if (arr2[0] == name) { return arr2[1]; } } return ''; } /*删除cookie*/ removeCookie (name) { this.setCookie(name, 1, -1); } /*-----------------localStorage---------------------*/ /*设置localStorage*/ setLocal(key, val) { var setting = arguments[0]; if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){ for(var i in setting){ this.ls.setItem(i, JSON.stringify(setting[i])) } }else{ this.ls.setItem(key, JSON.stringify(val)) } } /*获取localStorage*/ getLocal(key) { if (key) return JSON.parse(this.ls.getItem(key)) return null; } /*移除localStorage*/ removeLocal(key) { this.ls.removeItem(key) } /*移除所有localStorage*/ clearLocal() { this.ls.clear() } /*-----------------sessionStorage---------------------*/ /*设置sessionStorage*/ setSession(key, val) { var setting = arguments[0]; if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){ for(var i in setting){ this.ss.setItem(i, JSON.stringify(setting[i])) } }else{ this.ss.setItem(key, JSON.stringify(val)) } } /*获取sessionStorage*/ getSession(key) { if (key) return JSON.parse(this.ss.getItem(key)) return null; } /*移除sessionStorage*/ removeSession(key) { this.ss.removeItem(key) } /*移除所有sessionStorage*/ clearSession() { this.ss.clear() } }
9. Other 其它操作
/*获取网址参数*/ getURL(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = decodeURI(window.location.search).substr(1).match(reg); if(r!=null) return r[2]; return null; } /*获取全部url参数,并转换成json对象*/ getUrlAllParams (url) { var url = url ? url : window.location.href; var _pa = url.substring(url.indexOf('?') + 1), _arrS = _pa.split('&'), _rs = {}; for (var i = 0, _len = _arrS.length; i < _len; i++) { var pos = _arrS[i].indexOf('='); if (pos == -1) { continue; } var name = _arrS[i].substring(0, pos), value = window.decodeURIComponent(_arrS[i].substring(pos + 1)); _rs[name] = value; } return _rs; } /*删除url指定参数,返回url*/ delParamsUrl(url, name){ var baseUrl = url.split('?')[0] + '?'; var query = url.split('?')[1]; if (query.indexOf(name)>-1) { var obj = {} var arr = query.split("&"); for (var i = 0; i < arr.length; i++) { arr[i] = arr[i].split("="); obj[arr[i][0]] = arr[i][1]; }; delete obj[name]; var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&"); return url }else{ return url; } } /*获取十六进制随机颜色*/ getRandomColor () { return '#' + (function(h) { return new Array(7 - h.length).join("0") + h; })((Math.random() * 0x1000000 << 0).toString(16)); } /*图片加载*/ imgLoadAll(arr,callback){ var arrImg = []; for (var i = 0; i < arr.length; i++) { var img = new Image(); img.src = arr[i]; img.onload = function(){ arrImg.push(this); if (arrImg.length == arr.length) { callback && callback(); } } } } /*音频加载*/ loadAudio(src, callback) { var audio = new Audio(src); audio.onloadedmetadata = callback; audio.src = src; } /*DOM转字符串*/ domToStirng(htmlDOM){ var div= document.createElement("div"); div.appendChild(htmlDOM); return div.innerHTML } /*字符串转DOM*/ stringToDom(htmlString){ var div= document.createElement("div"); div.innerHTML=htmlString; return div.children[0]; } /** * 光标所在位置插入字符,并设置光标位置 * @param {dom} 输入框 * @param {val} 插入的值 * @param {posLen} 光标位置处在 插入的值的哪个位置 */ setCursorPosition (dom,val,posLen) { var cursorPosition = 0; if(dom.selectionStart){ cursorPosition = dom.selectionStart; } this.insertAtCursor(dom,val); dom.focus(); console.log(posLen) dom.setSelectionRange(dom.value.length,cursorPosition + (posLen || val.length)); } /*光标所在位置插入字符*/ insertAtCursor(dom, val) { if (document.selection){ dom.focus(); sel = document.selection.createRange(); sel.text = val; sel.select(); }else if (dom.selectionStart || dom.selectionStart == '0'){ let startPos = dom.selectionStart; let endPos = dom.selectionEnd; let restoreTop = dom.scrollTop; dom.value = dom.value.substring(0, startPos) + val + dom.value.substring(endPos, dom.value.length); if (restoreTop > 0){ dom.scrollTop = restoreTop; } dom.focus(); dom.selectionStart = startPos + val.length; dom.selectionEnd = startPos + val.length; } else { dom.value += val; dom.focus(); } }
CSS
1. pc-reset PC样式初始化