Files
portal/src/utils/index.js
2025-09-03 18:05:48 +08:00

37 lines
971 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 根据屏幕宽度自适应计算字体大小基于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')
)
}