gpt4 book ai didi

javascript - 风格化的控制台日志记录

转载 作者:行者123 更新时间:2023-12-02 22:47:54 26 4
gpt4 key购买 nike

当我在 Facebook 上并打开控制台时,我会看到下面的图片。他们是如何做到这一点的?

enter image description here

最佳答案

就像在 Firebug 中一样,您可以使用 %c 来设置控制台日志输出的样式。看看我们如何实现 Facebook 的示例:

console.log("%cStop!", "color: red; font-family: sans-serif; font-size: 4.5em; font-weight: bolder; text-shadow: #000 1px 1px;");

Facebook's example

由于它支持 CSS 属性,我们甚至可以在其中“绘制”图像:

(function(url) {
// Create a new `Image` instance
var image = new Image();

image.onload = function() {
// Inside here we already have the dimensions of the loaded image
var style = [
// Hacky way of forcing image's viewport using `font-size` and `line-height`
'font-size: 1px;',
'line-height: ' + this.height + 'px;',

// Hacky way of forcing a middle/center anchor point for the image
'padding: ' + this.height * .5 + 'px ' + this.width * .5 + 'px;',

// Set image dimensions
'background-size: ' + this.width + 'px ' + this.height + 'px;',

// Set image URL
'background: url('+ url +');'
].join(' ');

// notice the space after %c
console.log('%c ', style);
};

// Actually loads the image
image.src = url;
})('https://i.cloudup.com/Zqeq2GhGjt-3000x3000.jpeg');

Displaying an image on console log

关于javascript - 风格化的控制台日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26283936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com