Colors in JavaScript console

前端 未结 27 2186
说谎
说谎 2020-11-22 11:42

Can Chrome\'s built-in JavaScript console display colors?

I want errors in red, warnings in orange and console.log\'s in green. Is that possible?

27条回答
  •  伪装坚强ぢ
    2020-11-22 12:02

    Check this out:

    Animation in console, plus CSS

    (function() {
      var frame = 0;
      var frames = [
        "This",
        "is",
        "SPARTA!",
        " ",
        "SPARTA!",
        " ",
        "SPARTA!",
        " ",
        "SPARTA!",
        " ",
        "SPARTA!",
        " ",
        "SPARTA!"
      ];
      var showNext = () => {
        console.clear();
        console.log(
          `%c `,
          "background: red; color: white; font-size: 15px; padding: 3px 41%;"
        );
        console.log(
          `%c ${frames[frame]}`,
          "background: red; color: white; font-size: 25px; padding: 3px 40%;"
        );
        console.log(
          `%c `,
          "background: red; color: white; font-size: 15px; padding: 3px 41%;"
        );
        setTimeout(
          showNext,
          frames[frame] === "SPARTA!" || frames[frame] === " " ? 100 : 1500
        );
        // next frame and loop
        frame++;
        if (frame >= frames.length) {
          frame = 0;
        }
      };
      showNext();
    })();
    

    https://jsfiddle.net/a8y3jhfL/

    you can paste ASCII in each frame to watch an ASCII animation

提交回复
热议问题