一般未經修改的console.log僅僅是一行白底黑字的呈現而已
其實他不僅只能顯示白底黑字,加上以下介紹的CSS,讓你的console變得五彩繽紛
格式化標籤 | 說明 |
%s | 將數值格式化為字串。 |
%d?or?%i | 將數值格式化為整數。 |
%f | 將數值格式化為浮點數。 |
%o | 將數值格式化為可擴展的DOM元素(如在元素面板中)。 |
%O | 將數值格式化為可擴展的JavaScript物件。 |
%c | 根據提供的CSS樣式格式化輸出字串。 |
\n是換行,可以將一個字串設定成多行
%c標記之後的內容使用對應樣式,格式如 console.log(‘%c第一個樣式%c第二個樣式’,’css1′,’css2′);
如此對應樣式和普通的css效果一樣,可以設定文字顏色、背景顏色、字體大小、間距、邊距…等等。
還支援部分css3高級效果。
範例:
1.3D字體效果 Text
<script>console.log("%c3D Text"," text-shadow: 0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);font-size:5em")</script>
2.Colorful彩色背景 CSS
<script>console.log("%cColorful CSS","background: rgba(252,234,187,1);background: -moz-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%,rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: -webkit-gradient(left top, right top, color-stop(0%, rgba(252,234,187,1)), color-stop(12%, rgba(175,250,77,1)), color-stop(28%, rgba(0,247,49,1)), color-stop(39%, rgba(0,210,247,1)), color-stop(51%, rgba(0,189,247,1)), color-stop(64%, rgba(133,108,217,1)), color-stop(78%, rgba(177,0,247,1)), color-stop(87%, rgba(247,0,189,1)), color-stop(100%, rgba(245,22,52,1)));background: -webkit-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: -o-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: -ms-linear-gradient(left, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);background: linear-gradient(to right, rgba(252,234,187,1) 0%, rgba(175,250,77,1) 12%, rgba(0,247,49,1) 28%, rgba(0,210,247,1) 39%, rgba(0,189,247,1) 51%, rgba(133,108,217,1) 64%, rgba(177,0,247,1) 78%, rgba(247,0,189,1) 87%, rgba(245,22,52,1) 100%);filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fceabb', endColorstr='#f51634', GradientType=1 );font-size:5em")</script>
3.Rainbow彩虹字 Text
<script>console.log('%cRainbow Text ', 'background-image:-webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );color:transparent;-webkit-background-clip: text;font-size:5em;');</script>
4.顯示圖片
<script>console.log("%c", "padding:50px 700px;line-height:120px;background:url('https://img.chkaja.com/img/logo3.png') no-repeat;");</script>
實際範例1:
<script>console.log("%c%c網站%cKJ資訊站","line-height:28px;","line-height:28px;padding:4px;background:#222;color:#FADFA3;font-size:14px;","padding:4px;background:#FADFA3;color:#000;line-height:28px;font-size:14px;"); console.log("%c%c網站網址%chttps://www.chkaja.com","line-height:28px;","line-height:28px;padding:4px;background:#222;color:#FADFA3;font-size:14px;","padding:6px 6px 6px 2px;background:#FADFA3;color:#0093fff;line-height:28px;font-size:12px;"); console.log("%c%c站長%c月影星痕","line-height:28px;","line-height:28px;padding:4px;background:#222;color:#FADFA3;font-size:14px;","padding:4px;background:#FADFA3;color:red;line-height:28px;font-size:14px;"); console.log("%c%c五彩繽紛console.log!-----月影星痕","line-height:28px;","line-height:28px;padding:4px 0px;color:#fff;font-size:16px;background-image:-webkit-gradient(linear,left top,right top,color-stop(0,#ff22ff),color-stop(1,#5500ff));color:transparent;-webkit-background-clip:text;");</script>
實際範例2:
<script> console.log("%c%c網站%cKJ資訊站", "line-height:26px;", "line-height:16px;padding:2px 6px;border-radius: 5px 0px 0px 5px;background:#35495e;color:#fff;font-size:12px;", "padding:2px 8px;background:#0093ff;color:#fff;line-height:16px;font-size:12px;border-radius: 0px 5px 5px 0px;"); console.log("%c%c網站網址%chttps://www.chkaja.com", "line-height:26px;", "line-height:16px;padding:2px 6px;border-radius: 5px 0px 0px 5px;background:#35495e;color:#fff;font-size:12px;", "padding:2px 8px;background:#0093ff;color:#fff;line-height:16px;font-size:12px;border-radius: 0px 5px 5px 0px;"); console.log("%c%c站長%c月影星痕", "line-height:26px;", "line-height:16px;padding:2px 6px;border-radius: 5px 0px 0px 5px;background:#35495e;color:#fff;font-size:12px;", "padding:2px 8px;background:#0093ff;color:#fff;line-height:16px;font-size:12px;border-radius: 0px 5px 5px 0px;"); </script>
發佈留言