常用函数
去重:
1. (两个es6知识点set/...)
let arr = [1,2,3,4,1];
let set = new Set(arr)
// set 转 arr
let arr2 = [...set];
let arr3 = Array.from(set)
copyToClipboard - 复制到剪贴板
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
回到顶部:
const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};
ps:
scrollTo=scroll(但是官网建议频次高的话用scrollTop,scrollBy是相对定位)
function param2Obj(url) { const search = url.split('?')[1] if (!search) { return {} } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') + '"}' ) } function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000); document.cookie = `${cname}=${cvalue};expires=${d}}`; } function getCookie(cname){ var ca = document.cookie.split(';'); var name = cname +'='; for(var i=0;i<ca.length;i++){ var c = ca[i]; if(c.indexOf(name)==0){ return c.substring(name.length,c.length) } } }