console.log在控制台中打印出彩色

  • 第一个参数,需要写带有 %c 的字符串模板,代表你写的样式要作用的字符区域,可以连写多个:%c %c %c …
  • 后面的参数,需要你编写样式,就像写 CSS 样式一样,但是因为控制台支持的样式有限,类似于宽高之类的属性是不支持的,如果想实现宽高,可以使用 padding 来替代
  • 一行样式对应一个 %c
// 只有一个 %c 时
console.info(
  '%c this is me ',
  'background-color: #2196f3; padding: 6px 12px; border-radius: 2px; font-size: 14px; color: #fff; text-transform: uppercase; font-weight: 600;',
  window
);

// 两个 %c 时
console.info(
  '%c this is first %c this is second ',
  'background-color: #2196f3; padding: 6px 12px; border-radius: 2px; font-size: 14px; color: #fff; text-transform: uppercase; font-weight: 600;',
  'background-color: #00BCD4; padding: 6px 12px; border-radius: 2px; font-size: 14px; color: #fff; text-transform: uppercase; font-weight: 600;',
  window
);

console.log、console.info、console.warn、console.error 支持的样式:

background,包括 background-image 等
border,及其他border属性
border-radius
box-decoration-break
box-shadow
clear 和 float
color
cursor
display
font,及其他font属性
line-height
margin
outline,及其他outline属性
padding
text-transform
text-

white-space
word-spacing
word-break
writing-mode
*

猜你喜欢

转载自blog.csdn.net/LRQQHM/article/details/131062723