basic.js

1.数组

① 删除内容

Array.prototype.removeByValue = function(val) {
for(var i=0; i<this.length; i++) {
if(this[i] == val) {
this.splice(i, 1);
break;
}
}
}
var opsArr = ['name','kg','sex','mil'];
opsArr.removeByValue('mil');
console.log(opsArr); // ["name", "kg", "sex"]

② 获取 url上的键值对

export function urlParse(){
  let url = window.location.search;
  let reg =/[?&][^?&]+=[^&]+/g;
  let Arr = url.match(reg);
  let obj = {};
  if(Arr){
     Arr.forEach((item)=>{
     let temArr = item.substring(1).split('=');
     let key = decodeURIComponent(temArr[0]);
     let value = decodeURIComponent(temArr[1]);
     obj[key] = value;
     });
  }
return obj;
};

③ rem.js

// javascript
!(function () {
var widthScreen = 750
function getScreenWidth(){
var dl = document.documentElement.clientWidth
if(!dl){
dl = 340
return dl
}
return dl
}
var widthDl = getScreenWidth()
var sheet = (function () {
var style = document.createElement("style")
style.appendChild(document.createTextNode("html { font-size:" + (100*widthDl / widthScreen) + "px !important}"))
document.head.appendChild(style)
return style.sheet;
})();
window.addEventListener("resize", function () {
var dlResize = getScreenWidth()
sheet.insertRule("html { font-size:" + (100*dlResize / widthScreen) + "px !important}", sheet.rules.length);
sheet.deleteRule(0);
});
})();
 
④//判断是否有class
export function hasClass(el, className) {
let reg = new RegExp('(^|\\s)' + className + '(\\s|$)');
return reg.test(el.className);
}
⑤//添加class
export function addClass(el, className) {
if (hasClass(el, className)) {
return;
}
let newClass = el.className.split(' ');
newClass.push(className);
el.className = newClass.join(' ');
}
⑥//设置 /获取 自定义data- 属性的值
export function getData(el, name, val) {
const prefix = 'data-'
if (val) {
return el.setAttribute(prefix + name, val)
}
return el.getAttribute(prefix + name)
}
//⑦ 移除class
export function removeClass(el, className) {
if (!hasClass(el, className)) {
return
}
let newClass = el.className.split(' ');
newClass = newClass.splice(className,1);
el.className = newClass.join(' ');
}
let newClass = el.className.split(' ');
 
newClass.splice(newClass.indexOf(className),1);
el.className = newClass.join(' ');
}
//关于联动 前提引入jq
$('.menu').unbind('click').bind('click',function(){
  $(this).next().stop().slideToggle().siblings(".moremenu").slideUp();
  $(this).toggleClass("current").siblings(".menu").removeClass("current"); //current 父级变换图标
});
// 关于js 直接style css3 
比如: document.getElementById('div').style.transfrom =''
 
let elementStyle = document.createElement('div').style
let vendor = (() => {
let transformNames = {
webkit: 'webkitTransform',
Moz: 'MozTransform',
O: 'OTransform',
ms: 'msTransform',
standard: 'transform'
}
for (let key in transformNames) {
if (elementStyle[transformNames[key]] !== undefined) {
return key
}
}
return false
})()
export function prefixStyle(style) {
if (vendor === false) {
return false
}
if (vendor === 'standard') {
return style
}
return vendor + style.charAt(0).toUpperCase() + style.substr(1)
}
// 用法
const transform = prefixStyle('transform')
this.$refs.lyricList.$el.style[transform] = `translate3d(${offsetWidth}px,0,0)`
    
上一篇:Html5 web本地存储


下一篇:NPOI(2.1.3)向excel中插入图片,xls文档图片插入成功,xlsx文档图片插入失败