js获取随机数、日期格式化、全屏、首字母小写转大写、将十六进制颜色转换为 RGBA 格式

获取随机数

/**
 * 获取随机数
 * @param {*} min number 最小值
 * @param {*} max number 最大值
 * @returns number
 */
export const getRandomNum = (min = 0, max = 100) => {
  return Math.floor(Math.random() * (max - min)) + min
}

日期格式化

/**
 * 日期格式化
 * @param {*} date date 日期 new Date()
 * @param {*} fmt string 日期格式 'yyyy-MM-dd hh:mm:ss'
 * @returns string
 */
export const formatDate = (date = new Date(), fmt = 'yyyy-MM-dd hh:mm:ss') => {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      `${date.getFullYear()}`.substr(4 - RegExp.$1.length)
    )
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
  }
  // 遍历这个对象
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = `${o[k]}`
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      )
    }
  }
  return fmt
}

全屏

/** 全屏*/
export const toggleFullScreen = () => {
  if (
    !document.fullscreenElement &&
    !document.mozFullScreenElement &&
    !document.webkitFullscreenElement &&
    !document.msFullscreenElement
  ) {
    if (document.documentElement.requestFullscreen) {
      document.documentElement.requestFullscreen()
    } else if (document.documentElement.msRequestFullscreen) {
      document.documentElement.msRequestFullscreen()
    } else if (document.documentElement.mozRequestFullScreen) {
      document.documentElement.mozRequestFullScreen()
    } else if (document.documentElement.webkitRequestFullscreen) {
      document.documentElement.webkitRequestFullscreen(
        Element.ALLOW_KEYBOARD_INPUT
      )
    }
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen()
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen()
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen()
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen()
    }
  }
}

首字母小写转大写

//首字母小写转大写
export const firstLetterLowToUp = (text) => {
  return text.replace(text[0], text[0].toUpperCase())
}

将十六进制颜色转换为 RGBA 格式

/**
   * 将十六进制颜色转换为 RGBA 格式
   * @param {string} hex - 十六进制颜色值,例如 '#0080FF'
   * @param {number} alpha - 透明度值 (0 到 1 之间)
   * @returns {string} RGBA 颜色字符串
   */
  hexToRgba(hex, alpha) {
    // 去掉前导的 #
    hex = hex.replace(/^#/, '');

    // 如果是短格式颜色 (如 #FFF), 扩展成长格式 (如 #FFFFFF)
    if (hex.length === 4) {
      hex = hex.split('').map(function (char) {
        return char + char;
      }).join('');
    }

    // 解析红、绿、蓝的十六进制值
    const r = parseInt(hex.substring(0, 2), 16);
    const g = parseInt(hex.substring(2, 4), 16);
    const b = parseInt(hex.substring(4, 6), 16);

    // 返回 RGBA 颜色字符串
    return `rgba(${r}, ${g}, ${b}, ${alpha})`;
    /* // 示例用法
    const hexColor = '#0080FF';
    const alpha = 0.8;
    console.log(hexToRgba(hexColor, alpha)); // 输出: rgba(0, 128, 255, 0.8) */
  }

你可能感兴趣的:(前端,javascript)