博客
关于我
JS - 常用函数
阅读量:485 次
发布时间:2019-03-06

本文共 1480 字,大约阅读时间需要 4 分钟。

1. 生成随机颜色
function getRandomColor() {    return '#' + Math.floor(Math.random() * 16777215).toString(16);}
2.生成相近颜色
function sameColor(color: string, n: number) {        const hexDigits = color.length === 6 ? color.match(/(\d|[a-f])/g) : color.match(/(\d|[a-f])/g);        let newColor = '';        for (let i = 0; i < hexDigits.length; i++) {            const value = hexDigits[i];            const improvedValue = parseInt(value, 16);            const randomOffset = Math.random() * n;            const half = 0.5;            const variation = randomOffset > half ? -1 : 1;            const finalValue = Math.floor(improvedValue + variation * Math.random() * n);            newColor += finalValue.toString(16);        }        return newColor;    }
3. 获取url参数
function getParam(name: string) {        const reg = new RegExp('(^|&StartTime=' + name + '=(\\S+)&?', 'i');        const result = window.location.search.match(reg);        return result ? decodeURIComponent(result[2]) : null;    }
4. 将数字转成每隔3位加一个逗号
function addCommas(value: string) {        const num = value.replace('.', '');        const integPart = num.replace(/[^0-9]/g, '');        let result = '';        while (integPart.length > 3) {            result = ',' + integPart.slice(-3) + result;            integPart = integPart.slice(0, -3);        }        return integPart + (num.match(/\./) ? '.' + num.split('.').slice(1) : '');    }

转载地址:http://yhodz.baihongyu.com/

你可能感兴趣的文章
Nginx Lua install
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>
nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置https(一)—— 自签名证书
查看>>