寫在前面
本文中提到的部分設置需要 JS 權限,因此進行相關更改的時候,請確保有 JS 權限。
關於以下內容,是本人在自定義博客園的時候一點總結。這里有一個小技巧,善用瀏覽器的檢查
功能,我使用的是 Chrome,右鍵即可,這樣就可查看自己心儀的博客是如何進行更改美化的。
代碼高亮
以下給出幾種設置代碼高亮的方式,討論的編輯器僅在 TinyMCE 和 Markdown 進行過實驗,其他編輯器可自行嘗試。
- 方式一適用於 TinyMCE 編輯器
- 方式二適用於 Markdown 編輯器
- 方式三適用於 TinyMCE 編輯器
- 方式四適用於 Tiny MCE 編輯器
對於以上幾種方式,同時對 Markdown 與 TinyMCE 編輯器進行設置,不會產生沖突。
方式一:google-code-prettify
google-code-prettify 其詳細用法可參看其官方 GitHub
- 在博客設置頁面的頁腳HTML板塊添加如下代碼
<script src="https://files.cnblogs.com/files/nectar/prettify.js"></script>
<script type="text/javascript">
(function() {
$("pre").addClass("prettyprint");
prettyPrint();
})();
</script>
- 選擇配色
color themes for google-code-prettify 給出了一些配色樣式,可根據自己的喜歡,將對應 CSS 添加到博客設置頁面的頁面定制CSS代碼板塊 - 修改識別符號
博客園自帶的 SyntaxHighlighter 通過類似<pre class="brush:cpp;collapse:true;;gutter:true;">
來對代碼進行着色,而 google-code-prettify 通過類似<pre class="prettyprint lang-cpp">
來對代碼進行着色,因此,使用這種方式來為博客園設置代碼高亮,在插入代碼后,需要手動更新HTML代碼,將刷子換成 google-code-prettify 的識別符號,其中lang
表明語言。
點擊如圖示最右側的
HTML
圖標可修改HTML代碼。另外值得一提的是,對於之前使用TinyMCE編輯器並且是使用圖示右側的插入代碼來發布的代碼,pretty 渲染后代碼高亮會錯亂,<span>
標簽會全部顯示出來,因為沒有將其筆刷更改過來。有一種簡易的解決方案就是,使用圖示最左側的插入代碼來發布的代碼,不用手動更改筆刷,pretty 渲染后也不會錯亂。
這種方式,有一定優點,但是缺點也很明顯,使用右側的插入代碼不僅需要自己手動更改筆刷識別符號,而且與之前的隨筆可能並不兼容。為了保持兼容,所有隨筆需要使用左側的插入代碼來發布代碼。
方式二:highlight.js
highlight.js 提供了多種主題配色,更加詳細的使用方式可查看其官方 GitHub。下載 highlight.js包,選擇自己喜歡的配色,將對應 css 添加到博客設置頁面的頁面定制CSS代碼板塊。有一些樣式需要微調的,可根據需求自己更改,如果存在樣式沖突的話,偷懶的做法就是直接使用!important
強制覆蓋。我目前選擇的是 tomorrow.css 這個主題。
另一種使用方式就是Getting started中所講述的,在博客設置頁面的頁首HTML板塊中添加如下代碼:
<link rel="stylesheet" href="https://files.cnblogs.com/files/nectar/codehighlight.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
link
標簽中的href
指向的鏈接替換成自己喜歡的方案,可將方案上傳至自己博客園的文件中。
方式三:SyntaxHighlighter
目前對於這一插件的使用方式,我是直接使用的水郁的教程,在博客設置頁面的頁面定制CSS代碼板塊添加如下代碼:
.cnblogs_code,.cnblogs_code span{
font-size:15px!important;
}
.syntaxhighlighter a, .syntaxhighlighter div,
.syntaxhighlighter code, .syntaxhighlighter table,
.syntaxhighlighter table td, .syntaxhighlighter table tr,
.syntaxhighlighter table tbody, .syntaxhighlighter table thead,
.syntaxhighlighter table caption, .syntaxhighlighter textarea {
font-size: 15px!important;
}
.syntaxhighlighter .preprocessor {/*頭文件、預編譯部分*/
color: #e29710!important;
}
.syntaxhighlighter .keyword {/*關鍵字(for/if...else/while/return...)*/
font-weight: normal!important;
color: #b216ac!important;
}
.syntaxhighlighter .comments,
.syntaxhighlighter .comments a {/*注釋*/
color: #d84f19!important;
}
.syntaxhighlighter .color1,
.syntaxhighlighter .color1 a {/*int等變量類型名*/
color: #15b6bf!important;
}
.syntaxhighlighter .string,
.syntaxhighlighter .string a {/*字符串*/
color: #1bbb38!important;
}
.syntaxhighlighter .functions {/*scanf/printf等函數*/
color: #ff1493!important;
}
另外,你可以根據自己的需求進行更改,具體的樣式可參考 syntaxhighlighter_3.0.83 文件中的 styles 文件夾下的樣式,使用方式仍然是在博客設置頁面的頁面定制CSS代碼板塊添加樣式的 CSS 代碼。
方式四:SublimeHighlight插件
安裝 SublimeHighlight插件 后,在 Sublime Text 編輯器的Preferences > Package Settings > SublimeHighlight
菜單下進行用戶設置。一個用戶設置的例子如下:
{
"theme":"autumn", //代碼配色主題為 autumn
"full":false, //不導出完整HTML代碼
"linenos":"inline", //設置行號,默認導出的HTML代碼不設置行號
"fontface": "Monaco", //字體為 Monaco
"font-family": "Monaco"
}
更加詳細的使用方式可查看其官方 GitHub,之后點擊 Sublime Text 編輯器的Edit > Highlight > Convert to HTML
菜單導出 HTML 代碼,然后點擊編輯器的HTML
圖標(方式一中提到的)將導出的 HTML 代碼粘貼進去。
SublimeHighlight 提供了一些代碼配色方案,如下圖所示,當然你也可以在 博客幾種可選代碼配色方案一文中進行詳細查看。
自定義推薦|反對按鈕
將推薦|反對按鈕固定懸浮於屏幕右下角,在博客設置頁面的頁面定制CSS代碼板塊添加如下代碼:
#div_digg{
position:fixed;
bottom:5px;
width:140px;
right:30px;
border:2px solid #085;
padding:10px;
background-color:#fff;
opacity:0.3;
border-radius:5px 5px 5px 5px !important;
box-shadow:0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5);
transition-duration: 0.5s;
}
#div_digg:hover{
opacity:1;
}
添加目錄導航
為自己博文生成一個目錄導航,可在博客設置頁面的頁面定制CSS代碼板塊添加如下代碼:
/*生成博客目錄的CSS*/
#uprightsideBar{
font-size:18px;
font-family:'Monaco', Microsoft Yahei, 宋體, sans-serif;
text-align:left;
position:fixed;/*將div的位置固定到距離top:50px,left:0px的位置,這樣div就會處在最左邊的位置,距離頂部50px*/
top:50px;
left:0px;
width: auto;
height: auto;
}
#sideBarTab{
float:left;
width:30px;
border:1px solid #e5e5e5;
border-right:none;
text-align:center;
background:#ffffff;
}
#sideBarContents{
font-size:14px;
float:left;
overflow:auto;
overflow-x:hidden;!important;
width:200px;
min-height:108px;
max-height:460px;
border:1px solid #e5e5e5;
border-right:none;
background:#ffffff;
}
#sideBarContents dl{
margin:0;
padding:0;
}
#sideBarContents dt{
margin-top:5px;
margin-left:5px;
}
#sideBarContents dd, dt {
cursor: pointer;
}
#sideBarContents dd:hover, dt:hover {
color:#A7995A;
}
#sideBarContents dd{
margin-left:20px;
}
在博客設置頁面的頁首HTML代碼板塊添加如下代碼:
<script type="text/javascript">
var BlogDirectory = {
/*
獲取元素位置,距瀏覽器左邊界的距離(left)和距瀏覽器上邊界的距離(top)
*/
getElementPosition:function (ele) {
var topPosition = 0;
var leftPosition = 0;
while (ele){
topPosition += ele.offsetTop;
leftPosition += ele.offsetLeft;
ele = ele.offsetParent;
}
return {top:topPosition, left:leftPosition};
},
/*
獲取滾動條當前位置
*/
getScrollBarPosition:function () {
var scrollBarPosition = document.body.scrollTop || document.documentElement.scrollTop;
return scrollBarPosition;
},
/*
移動滾動條,finalPos 為目的位置,internal 為移動速度
*/
moveScrollBar:function(finalpos, interval) {
//若不支持此方法,則退出
if(!window.scrollTo) {
return false;
}
//窗體滾動時,禁用鼠標滾輪
window.onmousewheel = function(){
return false;
};
//清除計時
if (document.body.movement) {
clearTimeout(document.body.movement);
}
var currentpos =BlogDirectory.getScrollBarPosition();//獲取滾動條當前位置
var dist = 0;
if (currentpos == finalpos) {//到達預定位置,則解禁鼠標滾輪,並退出
window.onmousewheel = function(){
return true;
}
return true;
}
if (currentpos < finalpos) {//未到達,則計算下一步所要移動的距離
dist = Math.ceil((finalpos - currentpos)/10);
currentpos += dist;
}
if (currentpos > finalpos) {
dist = Math.ceil((currentpos - finalpos)/10);
currentpos -= dist;
}
var scrTop = BlogDirectory.getScrollBarPosition();//獲取滾動條當前位置
window.scrollTo(0, currentpos);//移動窗口
if(BlogDirectory.getScrollBarPosition() == scrTop)//若已到底部,則解禁鼠標滾輪,並退出
{
window.onmousewheel = function(){
return true;
}
return true;
}
//進行下一步移動
var repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")";
document.body.movement = setTimeout(repeat, interval);
},
htmlDecode:function (text){
var temp = document.createElement("div");
temp.innerHTML = text;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
},
/*
創建博客目錄,
id表示包含博文正文的 div 容器的 id,
mt 和 st 分別表示主標題和次級標題的標簽名稱(如 H2、H3,大寫或小寫都可以!),
interval 表示移動的速度
*/
createBlogDirectory:function (id, mt, st, interval){
//獲取博文正文div容器
var elem = document.getElementById(id);
if(!elem) return false;
//獲取div中所有元素結點
var nodes = elem.getElementsByTagName("*");
//創建博客目錄的div容器
var divSideBar = document.createElement('DIV');
divSideBar.className = 'uprightsideBar';
divSideBar.setAttribute('id', 'uprightsideBar');
var divSideBarTab = document.createElement('DIV');
divSideBarTab.setAttribute('id', 'sideBarTab');
divSideBar.appendChild(divSideBarTab);
var h2 = document.createElement('H2');
divSideBarTab.appendChild(h2);
var txt = document.createTextNode('目錄導航');
h2.appendChild(txt);
var divSideBarContents = document.createElement('DIV');
divSideBarContents.style.display = 'none';
divSideBarContents.setAttribute('id', 'sideBarContents');
divSideBar.appendChild(divSideBarContents);
//創建自定義列表
var dlist = document.createElement("dl");
divSideBarContents.appendChild(dlist);
var num = 0;//統計找到的mt和st
mt = mt.toUpperCase();//轉化成大寫
st = st.toUpperCase();//轉化成大寫
//遍歷所有元素結點
for(var i=0; i<nodes.length; i++)
{
if(nodes[i].nodeName == mt|| nodes[i].nodeName == st)
{
//獲取標題文本
var nodetext = nodes[i].innerHTML.replace(/<\/?[^>]+>/g,"");//innerHTML里面的內容可能有HTML標簽,所以用正則表達式去除HTML的標簽
nodetext = nodetext.replace(/ /ig, "");//替換掉所有的
nodetext = BlogDirectory.htmlDecode(nodetext);
//插入錨
nodes[i].setAttribute("id", "blogTitle" + num);
var item;
switch(nodes[i].nodeName)
{
case mt: //若為主標題
item = document.createElement("dt");
break;
case st: //若為子標題
item = document.createElement("dd");
break;
}
//創建錨鏈接
var itemtext = document.createTextNode(nodetext);
item.appendChild(itemtext);
item.setAttribute("name", num);
item.onclick = function(){ //添加鼠標點擊觸發函數
var pos = BlogDirectory.getElementPosition(document.getElementById("blogTitle" + this.getAttribute("name")));
if(!BlogDirectory.moveScrollBar(pos.top, interval)) return false;
};
//將自定義表項加入自定義列表中
dlist.appendChild(item);
num++;
}
}
if(num == 0) return false;
/*鼠標進入時的事件處理*/
divSideBarTab.onmouseenter = function(){
divSideBarContents.style.display = 'block';
}
/*鼠標離開時的事件處理*/
divSideBar.onmouseleave = function() {
divSideBarContents.style.display = 'none';
}
document.body.appendChild(divSideBar);
}
};
window.onload=function(){
/*頁面加載完成之后生成博客目錄*/
BlogDirectory.createBlogDirectory("cnblogs_post_body","h2","h3",20);
}
</script>
愛心特效
鼠標點擊頁面出現愛心特效,可在博客設置頁面博客側邊欄公告板塊添加如下代碼:
<!-- 愛心特效 -->
<script type="text/javascript">
(function(window,document,undefined){
var hearts = [];
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback){
setTimeout(callback,1000/60);
}
})();
init();
function init(){
css(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
attachEvent();
gameloop();
}
function gameloop(){
for(var i=0;i<hearts.length;i++){
if(hearts[i].alpha <=0){
document.body.removeChild(hearts[i].el);
hearts.splice(i,1);
continue;
}
hearts[i].y--;
hearts[i].scale += 0.004;
hearts[i].alpha -= 0.013;
hearts[i].el.style.cssText = "left:"+hearts[i].x+"px;top:"+hearts[i].y+"px;opacity:"+hearts[i].alpha+";transform:scale("+hearts[i].scale+","+hearts[i].scale+") rotate(45deg);background:"+hearts[i].color;
}
requestAnimationFrame(gameloop);
}
function attachEvent(){
var old = typeof window.onclick==="function" && window.onclick;
window.onclick = function(event){
old && old();
createHeart(event);
}
}
function createHeart(event){
var d = document.createElement("div");
d.className = "heart";
hearts.push({
el : d,
x : event.clientX - 5,
y : event.clientY - 5,
scale : 1,
alpha : 1,
color : randomColor()
});
document.body.appendChild(d);
}
function css(css){
var style = document.createElement("style");
style.type="text/css";
try{
style.appendChild(document.createTextNode(css));
}catch(ex){
style.styleSheet.cssText = css;
}
document.getElementsByTagName('head')[0].appendChild(style);
}
function randomColor(){
return "rgb("+(~~(Math.random()*255))+","+(~~(Math.random()*255))+","+(~~(Math.random()*255))+")";
}
})(window,document);
</script>
添加頭像
為自己的博客添加一個頭像,可在博客設置頁面博客側邊欄公告板塊添加如下代碼(鏈接更換成自己的頭像鏈接即可):
<img src="http://images2015.cnblogs.com/blog/885804/201610/885804-20161017233525654-1267575189.jpg" alt="zzx" class="img_avatar" width="230px" style="border-radius:50%">
評論帶頭像
在博客設置頁面的頁面定制CSS代碼板塊添加如下代碼:
/*評論頭像*/
.feedbackCon img {
border-radius: 30px;
-webkit-transition: all 0.6s ease-out;
-moz-transition: all 0.5s ease-out;
-ms-transition: all 0.5s ease-out;
-o-transition: all 0.5s ease-out;
transition: all 0.5s ease-out;
}
在博客設置頁面的頁腳HTML代碼板塊添加如下代碼:
function customTimer(inpId, fn) {
if ($(inpId).length) {
fn();
} else {
var intervalId = setInterval(function() {
if ($(inpId).length) { //如果存在了
clearInterval(intervalId); // 則關閉定時器
customTimer(inpId, fn); //執行自身
}
}, 100);
}
}
//添加 評論區的 形象照
function addImage() {
var spen_html = "<span class='bot' ></span>\
<span class='top'></span>";
$(".blog_comment_body").append(spen_html);
$(".blog_comment_body").before("<div class='body_right' style='float: left;'><a target='_blank'><img /></a></div>");
var feedbackCon = $(".feedbackCon").addClass("clearfix");
for (var i = 0; i < feedbackCon.length; i++) {
var span = $(feedbackCon[i]).find("span:last")[0].innerHTML || "http://pic.cnitblog.com/face/sample_face.gif";
$(feedbackCon[i]).find(".body_right img").attr("src", span);
var href = $(feedbackCon[i]).parent().find(".comment_date").next().attr("href");
$(feedbackCon[i]).find(".body_right a").attr("href", href);
}
}
//頁面加載完成是執行
$(function() {
//添加 評論區的 形象照
customTimer(".blog_comment_body", addImage);
});
返回頂部按鈕
在博客設置頁面的頁面定制CSS代碼板塊添加如下代碼:
.backToTop {
border: 1px dashed;
display: none;
width: 18px;
line-height: 1.2;
padding: 5px 0;
background-color: #fff;
color: #000;
font-size: 12px;
text-align: center;
position: fixed;
_position: absolute;
right: 10px;
bottom: 100px;
_bottom: "auto";
cursor: pointer;
opacity: .6;
filter: Alpha(opacity=60);
}
在博客設置頁面的頁首HTML代碼板塊添加如下代碼:
<script type="text/javascript">
(function() {
var $backToTopTxt = "返回頂部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
.text($backToTopTxt).attr("title", $backToTopTxt).click(function() {
$("html, body").animate({ scrollTop: 0 }, 120);
}), $backToTopFun = function() {
var st = $(document).scrollTop(), winh = $(window).height();
(st > 0)? $backToTopEle.show(): $backToTopEle.hide();
//IE6下的定位
if (!window.XMLHttpRequest) {
$backToTopEle.css("top", st + winh - 166);
}
};
$(window).bind("scroll", $backToTopFun);
$(function() { $backToTopFun(); });
})();
</script>
自定義表格樣式
包括自定義表格外觀樣式外,點擊標題頭,可選擇相應標題欄目對表格進行排序,此方法來源於博文純MarkDown博客閱讀體驗優化,樣式如下:
標題一 | 標題二 |
---|---|
內容1 | 內容3 |
內容2 | 內容4 |
在博客設置頁面的頁面定制CSS代碼板塊添加如下代碼:
/*表格*/
#cnblogs_post_body table
{
overflow:hidden;
border:1px solid #d3d3d3;
background:#fefefe;
margin:0 auto;
width: 95%;
border-left: 10px;
border-right: 10px;
border-radius:5px;
font-family: "Monaco","Microsoft YaHei";
}
#cnblogs_post_body table td
{
border: 0px;
font-size: 14px;
text-align: center;
border-top:1px solid #e0e0e0;
border-right:1px solid #e0e0e0;
}
#cnblogs_post_body table tr
{
background: #F7F7F7
}
#cnblogs_post_body table th
{
font-family: 'Monaco', '微軟雅黑', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
font-size: 15px;
text-align: center;
padding-top:5px;
text-shadow: 1px 1px 1px #fff;
background-color:#e8eaeb;
border: 0px;
}
/*表頭排序*/
table.tablesorter {
font-family:arial;
background-color: #CDCDCD;
margin:10px 0pt 15px;
font-size: 8pt;
width: 100%;
text-align: left;
}
table.tablesorter thead tr th, table.tablesorter tfoot tr th {
background-color: #e6EEEE;
border: 1px solid #FFF;
font-size: 8pt;
padding: 4px;
}
table.tablesorter thead tr .header {
background-image: url(https://files.cnblogs.com/files/nectar/bg.gif);
background-repeat: no-repeat;
background-position: center right;
cursor: pointer;
}
table.tablesorter tbody td {
color: #3D3D3D;
padding: 4px;
background-color: #FFF;
vertical-align: top;
}
table.tablesorter tbody tr.odd td {
background-color:#F0F0F6;
}
table.tablesorter thead tr .headerSortUp {
background-image: url(https://files.cnblogs.com/files/nectar/asc.gif);
}
table.tablesorter thead tr .headerSortDown {
background-image: url(https://files.cnblogs.com/files/nectar/desc.gif);
}
table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
background-color: #8dbdd8;
}
在博客設置頁面的頁腳HTML代碼板塊添加如下代碼:
<!-- 表頭排序-->
<script type="text/javascript" src="https://files.cnblogs.com/files/nectar/jquery_table_sort.js"></script>
<script>
$(document).ready(function()
{
$("table").addClass("tablesorter");
$("table").tablesorter();
}
);
</script>
寫在最后
在美化博客園樣式的道路其實踩了很多坑,這里很感謝水郁,從 Ta 的博客中學到了很多技巧。另外,我現在使用的博客園整體的樣式基本參考自水郁,只是對一些地方進行了自我修改,當然,在使用其樣式的時候,我有征詢過水郁博主的同意。
對於本博客修改的css樣式可直接獲取:CnblogsTheme