【新】常用编程技巧总结(持续更新)

​​​​​​【新】常用编程技巧总结(持续更新)


华丽分割线


Apache

Apache设置支持跨域请求,解决【No 'Access-Control-Allow-Origin' header】

  • 打开httpd.conf文件
  • 开启【LoadModule headers_module modules/mod_headers.so】
LoadModule headers_module modules/mod_headers.so

  • 设置头部参数
<IfModule headers_module>
	Header set Access-Control-Allow-Origin: "*"
	Header set Access-Control-Allow-Methods: "GET,POST,PUT,DELETE,OPTIONS"
	Header set Access-Control-Allow-Headers: "Content-Type"
</IfModule>

  • 重启Apache服务器
httpd.exe -k restart

华丽分割线


原生纯JS

生成6位16进制随机颜色值(count:3位|6位)

function randomColor(count = 6) {
	return "#" + Math.random().toString(16).substr(2, count);
}

任意颜色取反色(color:颜色值,count:3位|6位)

function reverseColor(color, count = 6) {
	var c = color.toString().replace(/#/g, "").substr(0, count);
	c = 0xFFFFFF - ("0x" + c.toString(16));
	return c.toString(16).substr(6 - count, count);;
}

生成任意区间【n,m】的随机数

function random(n, m) {
	return parseInt(Math.random() * (m - n + 1) + n, 10);
}

任意时刻钟表时针、分针、秒针旋转的角度

function clockRotate() {
	var d = new Date();
	// 秒针
	var seconds = d.getSeconds() / 60 * 360;
	//分针
	var minutes = (d.getMinutes() * 60 * 1000 + d.getSeconds() * 1000) / (60 * 60 * 1000) * 360;
	//时针
	var dd = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0);
	var hours = (d.getTime() - dd.getTime()) / (24 * 60 * 60 * 1000) * 720;
	return {
		seconds: seconds,
		minutes: minutes,
		hours: hours
	};
}

猜你喜欢

转载自blog.csdn.net/starzhangkiss/article/details/90693883