初始化代码
This commit is contained in:
36
src/utils/index.js
Normal file
36
src/utils/index.js
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* 根据屏幕宽度自适应计算字体大小(基于1920设计稿)
|
||||
* @param {number} res - 设计稿上的原始尺寸
|
||||
* @returns {number} 自适应后的尺寸
|
||||
*/
|
||||
export function fontSize(res) {
|
||||
let clientWidth =
|
||||
window.innerWidth ||
|
||||
document.documentElement.clientWidth ||
|
||||
document.body.clientWidth
|
||||
if (!clientWidth) return
|
||||
let fontSize = clientWidth / 1920
|
||||
return res * fontSize
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机背景色
|
||||
* @param {String} type 'hex' | 'rgb' 输出格式,默认 hex
|
||||
* @returns {String} '#RRGGBB' 或 'rgb(r,g,b)'
|
||||
* @example randomBgColor()
|
||||
*/
|
||||
export function randomBgColor(type = 'hex') {
|
||||
if (type === 'rgb') {
|
||||
const r = Math.floor(Math.random() * 256)
|
||||
const g = Math.floor(Math.random() * 256)
|
||||
const b = Math.floor(Math.random() * 256)
|
||||
return `rgb(${r},${g},${b})`
|
||||
}
|
||||
// 默认 hex
|
||||
return (
|
||||
'#' +
|
||||
Math.floor(Math.random() * 0xffffff)
|
||||
.toString(16)
|
||||
.padStart(6, '0')
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user