Hexo部落格主題優化

標題圖
Hexo
部落格主題優化
新增背景圖
在 themes/*/source/css/_custom/custom.styl
中新增如下程式碼:
body{ background:url(/images/bg.jpg); background-size:cover; background-repeat:no-repeat; background-attachment:fixed; background-position:center; }
修改Logo字型
在 themes/*/source/css/_custom/custom.styl
中新增如下程式碼:
@font-face { font-family: Zitiming; src: url('/fonts/##.ttf.ttf'); } .site-title { font-size: 40px !important; font-family: '##' !important; }
檔案在 themes/next/source/fonts
目錄下,保留字型檔案。
本地文章優化
npm install hexo-generator-sitemap --save npm install hexo-generator-baidu-sitemap --save
站點配置檔案 _config.yml
中新增以下程式碼
sitemap: path: sitemap.xml baidusitemap: path: baidusitemap.xml
站點配置檔案 _config.yml
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/' url: http://你的網站
新增文章書寫樣式

效果
開啟 themes/next/source/css/_custom
下的 custom.styl
檔案
// 黃 span#inline-yellow { display:inline; padding:.2em .6em .3em; font-size:80%; font-weight:bold; line-height:1; color:#fff; text-align:center; white-space:nowrap; vertical-align:baseline; border-radius:0; background-color: #f0ad4e; } // 綠 span#inline-green { display:inline; padding:.2em .6em .3em; font-size:80%; font-weight:bold; line-height:1; color:#fff; text-align:center; white-space:nowrap; vertical-align:baseline; border-radius:0; background-color: #5cb85c; } // 藍 span#inline-blue { display:inline; padding:.2em .6em .3em; font-size:80%; font-weight:bold; line-height:1; color:#fff; text-align:center; white-space:nowrap; vertical-align:baseline; border-radius:0; background-color: #2780e3; } // 紫 span#inline-purple { display:inline; padding:.2em .6em .3em; font-size:80%; font-weight:bold; line-height:1; color:#fff; text-align:center; white-space:nowrap; vertical-align:baseline; border-radius:0; background-color: #9954bb; }
編輯的文章地方
<span id="inline-yellow"> 站點配置檔案 </span> <span id="inline-blue"> 站點配置檔案 </span> <span id="inline-green"> 主題配置檔案 </span> <span id="inline-purple"> 主題配置檔案 </span>
頭像改成圓形
themes/next/source/css/_common/components/sidebar/sidebar-author.styl
中 .site-author-image
定義
border-radius: 50%; -webkit-border-radius: 50%; -moz-border-radius: 50%;
robots.txt
檔案
hexo
站點的 source
檔案,在 source
資料夾中新建檔案 robots.txt
User-agent: * Allow: / Allow: /archives/ Disallow: /vendors/ Disallow: /categories/ Sitemap: http://www../sitemap.xml Sitemap: http://www../baidusitemap.xml
# hexo robots.txt User-agent: * Allow: / Allow: /archives/ Disallow: /vendors/ Disallow: /js/ Disallow: /css/ Disallow: /fonts/ Disallow: /vendors/ Disallow: /fancybox/ Sitemap: http://www..com/sitemap.xml Sitemap: http://www..com/baidusitemap.xml
修改內容的寬度
source/css/_variables/custom.styl
檔案
// 修改成你期望的寬度 $content-desktop = 700px // 當視窗超過 1600px 後的寬度 $content-desktop-large = 900px
文章加密
themes/*/layout/_partials/head.swig
檔案
<script> (function(){ if('{{ page.password }}'){ if (prompt('請輸入密碼') !== '{{ page.password }}'){ alert('密碼錯誤'); history.back(); } } })(); </script>
文章上
--- password: 123456 ---
新增頭像
themes/next
下的 _config.yml
檔案
avatar: http://example.com/avatar.png
或者使用本地圖片,圖片把放入 themes/next/source/images
下
avatar: /images/vator.png
頭像旋轉
.site-author-image { display: block; margin: 0 auto; padding: $site-author-image-padding; max-width: $site-author-image-width; height: $site-author-image-height; border: $site-author-image-border-width solid $site-author-image-border-color; // 修改頭像邊框 border-radius: 50%; -webkit-border-radius: 50%; -moz-border-radius: 50%; // 設定旋轉 transition: 1.4s all; } // 可旋轉的圓形頭像 .site-author-image:hover { -webkit-transform: rotate(360deg); -moz-transform: rotate(360deg); -ms-transform: rotate(360deg); -transform: rotate(360deg); }
點擊出現桃心效果
/themes/*/source/js/src
下新建檔案 click.js
!function(e,t,a){function n(){c(".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: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
\themes\*\layout\_layout.swig
檔案末尾新增
<!-- 頁面點選小紅心 --> <script type="text/javascript" src="/js/src/clicklove.js"></script>
修改訪問URL路徑
Hexo
站點下的 _config.yml
檔案
permalink: :category/:title/
建立標籤目錄
$ cd you-site $ hexo new page tags
建立後,開啟 you-site/source/tags
的 index.md
--- title:#頁面主題 date:#當前建立檔案時間 type: "tags" # 設定頁面型別 ---
RSS
$ npm install hexo-generator-feed --save
Hexo
站點下的 _config.yml
# feed # Dependencies: https://github.com/hexojs/hexo-generator-feed feed: type: atom path: atom.xml limit: 20 hub: content:
文章新增邊框陰影
開啟 themes/*/source/css/_custom/custom.styl
// 主頁文章新增陰影 .post { margin-top: 0px; margin-bottom: 60px; padding: 25px; -webkit-box-shadow: 0 0 5px rgba(202, 203, 203, .5); -moz-box-shadow: 0 0 5px rgba(202, 203, 204, .5); }
顯示當前瀏覽進度
themes/*/_config.yml
# Back to top in sidebar b2t: true # Scroll percent label in b2t button scrollpercent: true
修改文章間分割線
themes/next/source/css/_common/components/post/
下的 post-eof.styl
.posts-expand { .post-eof { display: block; //margin: $post-eof-margin-top auto $post-eof-margin-bottom; width: 0%; //分割線長度 height: 0px; // 分割線高度 background: $grey-light; text-align: center; } }
開啟版權宣告
主題配置檔案下
# Declare license on posts post_copyright: enable: true license: CC BY-NC-SA 3.0 license_url: https://creativecommons.org/licenses/by-nc-sa/3.0/
帶#號的標籤
themes/next/layout/_macro/
下的 post.swig
檔案
<div class="post-tags"> {% for tag in post.tags %} <a href="{{ url_for(tag.path) }}" rel="tag"><i class="fa fa-tag"></i> {{ tag.name }}</a> {% endfor %} </div>
文章字數統計,閱讀時長
$ npm i --save hexo-wordcount
themes/next
下的 _config.yml
# Dependencies: https://github.com/willin/hexo-wordcount post_wordcount: item_text: true #字數統計 wordcount: true #預覽時間 min2read: true #總字數 totalcount: false separated_meta: true
連線樣式
themes/next/source/css/_custom/
下的 custom.styl
.post-body p a{ color: #0593d3; border-bottom: none; border-bottom: 1px solid #0593d3; &:hover { color: #fc6423; border-bottom: none; border-bottom: 1px solid #fc6423; } }
新增廣告
註冊,複製程式碼,在 theme/*/layout/_custom/google_ad.swig
, AdSense 上的程式碼貼上進去,複製一份 theme/*/layout/_custom/head.swig
在 theme/*/layout/post.swig
新增:
{% include '_custom/google_ad.swig' %}
{% block content %} <div id="posts" class="posts-expand"> {{ post_template.render(page) }} {% include '_custom/google_ad.swig' %} </div> {% endblock %}
頂部載入條
themes/next
下的 _config.yml
pace: true # Themes list: #pace-theme-big-counter #pace-theme-bounce #pace-theme-barber-shop #pace-theme-center-atom #pace-theme-center-circle #pace-theme-center-radar #pace-theme-center-simple #pace-theme-corner-indicator #pace-theme-fill-left #pace-theme-flash #pace-theme-loading-bar #pace-theme-mac-osx #pace-theme-minimal # For example # pace_theme: pace-theme-center-simple pace_theme: pace-theme-flash
新增複製功能
下載外掛 clipboard.js
在 themes/next/source/lib/
,下新建 clipboard
資料夾
把 src
資料夾下的檔案拖動到 clipboard
資料夾
themes/next/source/js/src/
,建立 custom.js
function createCopyBtns() { var $codeArea = $("figure table"); if ($codeArea.length > 0) { //複製成功後將要乾的事情 function changeToSuccess(item) { $imgOK = $("#copyBtn").find("#imgSuccess"); if ($imgOK.css("display") == "none") { $imgOK.css({ opacity: 0, display: "block" }); $imgOK.animate({ opacity: 1 }, 1000); setTimeout(function() { $imgOK.animate({ opacity: 0 }, 2000); }, 2000); setTimeout(function() { $imgOK.css("display", "none"); }, 4000); }; }; $(".post-body").before('<div id="copyBtn" style="opacity: 0; position: absolute;top:0px;display: none;line-height: 1; font-size:1.5em"><span id="imgCopy" ><i class="fa fa-paste fa-fw"></i></span><span id="imgSuccess" style="display: none;"><i class="fa fa-check-circle fa-fw" aria-hidden="true"></i></span>'); var clipboard = new Clipboard('#copyBtn', { target: function() { return document.querySelector("[copyFlag]"); }, isSupported: function() { return document.querySelector("[copyFlag]"); } }); clipboard.on('success', function(e) { //清除內容被選擇狀態 e.clearSelection(); changeToSuccess(e); }); clipboard.on('error', function(e) { console.error('Action:', e.action); console.error('Trigger:', e.trigger); }); //滑鼠 $("#copyBtn").hover( function() { $(this).stop(); $(this).css("opacity", 1); }, function() { $(this).animate({ opacity: 0 }, 2000); } ); } } $("figure").hover( function() { //移除之前含有複製標誌程式碼塊的 copyFlag $("[copyFlag]").removeAttr("copyFlag"); //在新的(當前滑鼠所在程式碼區)程式碼塊插入標誌:copyFlag $(this).find(".code").attr("copyFlag", 1); $copyBtn = $("#copyBtn"); if ($copyBtn.lenght != 0) { $copyBtn.stop(); $copyBtn.css("opacity", 0.8); $copyBtn.css("display", "block"); $copyBtn.css("top", parseInt($copyBtn.css("top")) + $(this).offset().top - $copyBtn.offset().top + 3); $copyBtn.css("left", -$copyBtn.width() - 3); } }, function() { $("#copyBtn").animate({ opacity: 0 }, 2000); } ); $(document).ready(function() { createCopyBtns(); });
themes/next/layout/_custom/
建立 custom.swig
<script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script> <script type="text/javascript" src="/js/src/custom.js"></script>
themes/next/layout/_layout.swig
,標籤在 </body>
上面
{% include '_custom/custom.swig' %}
我的部落格
個人部落格:see_no_evil:: 個人部落格
如果看了覺得不錯
點贊!轉發!
達叔小生:往後餘生,唯獨有你
You and me, we are family !
90後帥氣小夥,良好的開發習慣;獨立思考的能力;主動並且善於溝通
簡書部落格: 達叔小生
https://www.jianshu.com/u/c785ece603d1結語
- 下面我將繼續對 其他知識 深入講解 ,有興趣可以繼續關注
- 小禮物走一走 or 點贊