動態顯示目錄的作用 不用每次寫博客的時候繁瑣的人工整理目錄,又可以動態浮動在右下角,方便快速跳到感興趣的位置同時也可以快速的對文章內容有一個大概的瞭解。 實現原理 首先根據個人喜好,我習慣了用 h1 來做分類。所以本篇內容也主要是針對h1來提取目錄。 如何提取出來h1呢? 先來看這張圖,以獵豹瀏覽器 ...
動態顯示目錄的作用
不用每次寫博客的時候繁瑣的人工整理目錄,又可以動態浮動在右下角,方便快速跳到感興趣的位置同時也可以快速的對文章內容有一個大概的瞭解。
實現原理
首先根據個人喜好,我習慣了用 h1 來做分類。所以本篇內容也主要是針對h1來提取目錄。
如何提取出來h1呢?
先來看這張圖,以獵豹瀏覽器為例:
首先在博客內容第一行點擊滑鼠右鍵,然後選擇檢查。這時會彈出右邊的框,直接定位到我的h1標簽,就這麼簡單的找到了它的父級 cnblogs_post_body 。
然後使用 jquery 選擇器 來獲取到這些h1,對jquery選擇器不熟的直接跳這個鏈接溫習一下:http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp
$('#cnblogs_post_body h1')
就這麼簡單的一個括弧就完成了對 h1 的提取。
在遍歷所有的h1,取出內容之前,我們需要一個目錄的容器。
$('#cnblogs_post_body').append('<div id="blog_catalog" class="blog_catalog"><ul><li><a id="blog_catalog_close" class="blog_catalog_close">>摺疊目錄</a></li></ul></div>');
這句簡單解釋就是在博客內容最末尾插入了一個 div,裡面包含ul和一個預設的li 用於摺疊目錄。
接下來就要提取h1的內容了,但在這個過程中我們還要做一件事,就是自動給h1增加一個id,作為一個標準的懶人,我肯定連h1的id都不想寫的,自動生成神馬的最好了。
var id = 1; $('#cnblogs_post_body h1').each(function(){ $(this).attr('id','blog_catalog_id_'+id); $('#blog_catalog ul').append('<li><a href="#blog_catalog_id_'+id+'">'+$(this).text()+'</a></li>'); id++; });
有了摺疊,當然少不了展開。
$('#cnblogs_post_body').append('<div id="blog_catalog_open" class="blog_catalog_open">展開目錄</div>');
最後一步,讓展開目錄和摺疊目錄聯動起來
$('#blog_catalog_open').click(function(){ $('#blog_catalog').show(); $('#blog_catalog_open').hide(); }); $('#blog_catalog_close').click(function(){ $('#blog_catalog').hide(); $('#blog_catalog_open').show(); });
整個製作過程其實並不複雜,還有一些css樣式應用上就完工了。
不管原理,我想直接用
怎麼自定義皮膚我就不多說了,一抓一大把。下麵直接給你們代碼。
css:
.blog_catalog { display: none; width: auto; height: auto; float: right; position: fixed; right: 180px; bottom: 200px; z-index: 9999; background-color: #fff; font-size: 12px; margin: 10px 0 0 0; padding: 5px; text-align: center; border: 3px solid #55895b; border-radius: 5px; -webkit-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13); -moz-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13); box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13); } .blog_catalog > li > a { background-color: #616975; background-image: -webkit-gradient(linear, left top, left bottom, from(rgb(114, 122, 134)),to(rgb(80, 88, 100))); background-image: -webkit-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100)); background-image: -moz-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100)); background-image: -o-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100)); background-image: -ms-linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100)); background-image: linear-gradient(top, rgb(114, 122, 134), rgb(80, 88, 100)); filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,StartColorStr='#727a86', EndColorStr='#505864'); -webkit-box-shadow: inset 0px 1px 0px 0px #878e98; -moz-box-shadow: inset 0px 1px 0px 0px #878e98; box-shadow: inset 0px 1px 0px 0px #878e98; width: 100%; height: 2.75em; line-height: 2.75em; text-indent: 2.75em; display: block; position: relative; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-weight: 600; color: #fff; text-shadow: 0px 1px 0px rgba(0,0,0,.5); } .blog_catalog ul li a { background: #fff; border-bottom: 1px solid #efeff0; width: 100%; height: 2.75em; line-height: 2.75em; display: block; position: relative; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 0.923em; font-weight: 400; color: #878d95; } .blog_catalog ul li a:hover { cursor: pointer; } .blog_catalog > li > a:hover, .blog_catalog > li > a.active, .blog_catalog > li:target > a; /*add this*/ { background-color: #35afe3; background-image: -webkit-gradient(linear, left top, left bottom, from(rgb(69, 199, 235)),to(rgb(38, 152, 219))); background-image: -webkit-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219)); background-image: -moz-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219)); background-image: -o-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219)); background-image: -ms-linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219)); background-image: linear-gradient(top, rgb(69, 199, 235), rgb(38, 152, 219)); filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0,StartColorStr='#45c7eb', EndColorStr='#2698db'); border-bottom: 1px solid #103c56; -webkit-box-shadow: inset 0px 1px 0px 0px #6ad2ef; -moz-box-shadow: inset 0px 1px 0px 0px #6ad2ef; box-shadow: inset 0px 1px 0px 0px #6ad2ef; } .blog_catalog > li > a.active { border-bottom: 1px solid #1a638f; } .blog_catalog > li > a:before { content: ''; background-image: url(../images/sprite.png); background-repeat: no-repeat; font-size: 36px; height: 1em; width: 1em; position: absolute; left: 0; top: 50%; margin: -.5em 0 0 0; } .item1 > a:before { background-position: 0 0; } .item2 > a:before { background-position: -38px 0; } .item3 > a:before { background-position: 0 -38px; } .item4 > a:before { background-position: -38px -38px; } .item5 > a:before { background-position: -76px 0; } .blog_catalog > li > a span { font-size: 0.857em; display: inline-block; position: absolute; right: 1em; top: 50%; background: #48515c; line-height: 1em; height: 1em; padding: .4em .6em; margin: -.8em 0 0 0; color: #fff; text-indent: 0; text-align: center; -webkit-border-radius: .769em; -moz-border-radius: .769em; border-radius: .769em; -webkit-box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, .26), 0px 1px 0px 0px rgba(255, 255, 255, .15); -moz-box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, .26), 0px 1px 0px 0px rgba(255, 255, 255, .15); box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, .26), 0px 1px 0px 0px rgba(255, 255, 255, .15); text-shadow: 0px 1px 0px rgba(0,0,0,.5); font-weight: 500; } .blog_catalog > li > a:hover span, .blog_catalog > li a.active span, .blog_catalog > li:target > a span /*add this*/ { background: #2173a1; } .blog_catalog > li > ul li a:before { font-size: 8px; color: #bcbcbf; position: absolute; width: 1em; height: 1em; top: 0; left: -2.7em; } .blog_catalog > li > ul li:hover a, .blog_catalog > li > ul li:hover a span, .blog_catalog > li > ul li:hover a:before { color: #32373D; } .blog_catalog ul > li > a span { font-size: 0.857em; display: inline-block; position: absolute; right: 1em; top: 50%; / background: #fff; border: 1px solid #d0d0d3; line-height: 1em; height: 1em; padding: .4em .7em; margin: -.9em 0 0 0; color: #878d95; text-indent: 0; text-align: center; -webkit-border-radius: .769em; -moz-border-radius: 769em; border-radius: 769em; text-shadow: 0px 0px 0px rgba(255,255,255,.01)); } /*additional*/ .blog_catalog > li > ul { height: 0; overflow: hidden; opacity: 0; filter: alpha(opacity=0); /* IE6-IE8 */ -webkit-transition: opacity 0.9s ease-in-out; -moz-transition: opacity 0.9s ease-in-out; -o-transition: opacity 0.9s ease-in-out; -ms-transition: opacity 0.9s ease-in-out; transition: opacity 0.9s ease-in-out; } .blog_catalog > li:target > ul { height: auto; /*using auto nullifies the height transitions, but it makes things flexible which is more important*/ border-bottom: 1px solid #51555a; opacity: 1; filter: alpha(opacity=100); /* IE6-IE8 */ } #cnblogs_post_body ul li { list-style-type: none; margin-left: -30px; } .blog_catalog_open { width: auto; height: auto; float: right; position: fixed; right: 180px; bottom: 200px; z-index: 9999; background-color: #fff; font-size: 12px; width: 125px; margin: 10px 0 0 0; padding: 5px; text-align: center; border: 3px solid #55895b; border-radius: 5px; -webkit-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13); -moz-box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13); box-shadow: 0px 1px 3px 0px rgba(0,0,0,.73), 0px 0px 18px 0px rgba(0,0,0,.13); cursor: pointer; }View Code
js:
$('#cnblogs_post_body').append('<div id="blog_catalog_open" class="blog_catalog_open">展開目錄</div>'); $('#cnblogs_post_body').append('<div id="blog_catalog" class="blog_catalog"><ul><li><a id="blog_catalog_close" class="blog_catalog_close">>摺疊目錄</a></li></ul></div>'); var id = 1; $('#cnblogs_post_body h1').each(function(){ $(this).attr('id','blog_catalog_id_'+id); $('#blog_catalog ul').append('<li><a href="#blog_catalog_id_'+id+'">'+$(this).text()+'</a></li>'); id++; }); $('#blog_catalog_open').click(function(){ $('#blog_catalog').show(); $('#blog_catalog_open').hide(); }); $('#blog_catalog_close').click(function(){ $('#blog_catalog').hide(); $('#blog_catalog_open').show(); });View Code
如何使用
<h1>你可以隨意設置你的標題</h1>
沒錯,就是這麼簡單,其他什麼都不需要。
我不只要用目錄,我還要完整皮膚
先聲明,我這套皮膚是從別人那扒下來的一個雛形然後自己再改了不少地方。但是從哪裡弄來的,忘了。。。很尷尬。
具體設置皮膚的也是一抓一大把就不多說了,上乾貨。
1.博客皮膚 選擇 Custom
2.頁面定製css代碼
@charset "utf-8"; /* CSS Document */ /* By rhinoc.cnblogs.com*/ /*第一部分*/ #EntryTag { margin-top: 20px; font-size: 9pt; color: gray; } .topicListFooter { text-align: right; margin-right: 10px; margin-top: 10px; } #divRefreshComments { text-align: right; margin-right: 10px; margin-bottom: 5px; font-size: 9pt; } /*全局樣式*/ { margin: 0; padding: 0; } html { height: 100%; } body { background-image: url(http://images0.cnblogs.com/blog2015/618672/201508/201613472223856.png); background-repeat: repeat; font-family: 'Lucida Console',Georgia,'Microsoft YaHei',Microsoft YaHei; \5B8B\4F53, sans-serif; font: 'Lucida Console',Georgia,'Microsoft YaHei',Microsoft YaHei; font-size: 11.5px; min-height: 101%; } table { border-collapse: collapse; border-spacing: 0; } fieldset, img { border: 0; } ul { word-break: break-all; } li { list-style: none; } h1, h2, h3, h4, h5, h6 { font-size: 100%; font-weight: normal; } a { outline: none; color: #21759b; } address, cite, dfn, em, var { font-style: normal; } code, kbd, pre, samp, tt { font-family: "Courier New", Courier,Microsoft Yahei, monospace; } .clear { clear: both; } /*第三部分*/ /*home和頭部*/ #home { margin: 0 auto; width: 65%; min-width: 1000px; background-color: #fff; padding: 30px; margin-top: 50px; margin-bottom: 50px; box-shadow: 0px 1px 10px #999; -moz-box-shadow: 0px 1px 10px #999; -web-kit-shadow: 0px 1px 10px #999; } #header { padding-bottom: 5px; margin-top: 20px; } #blogTitle { height: 50px; clear: both; font-family: Georgia,Serif; } #InkBlogLogo { display: none; } /*博客名稱*/ #blogTitle h1 { font-size: 28px; font-weight: bold; line-height: 0.2em; margin-top: 20px; } #blogTitle h1 a { color: #515151; } #blogTitle h1 a:hover { color: #21759b; } #blogTitle h2 { font-weight: normal; font-size: 14.5px; line-height: 0.3em; color: #515151; float: left; margin-left: 2em; margin-bottom: 2em; } #blogLogo { float: right; } /*導航欄*/ #navigator { text-decoration: none; font-size: 14px; font-family: 'Lucida Console',Georgia,'FZYaoTi',Microsoft YaHei; \5B8B\4F53, sans-serif; font: 'Lucida Console',Georgia,'FZYaoTi',Microsoft YaHei; border-bottom: 1px solid #515151; border-top: 1px solid #515151; height: 80px; clear: both; margin-top: 20px; } #navList { width: 1200px; min-height: 30px; float: left; } #navList .border { height: 28px; position: absolute; width: 5px; left: 0px; top: 0px; overflow: hidden; opacity: 0; background: #F90; -webkit-transition: 0.3s all ease; -moz-transition: 0.3s all ease; -ms-transition: 0.3s all ease; -o-transition: 0.3s all ease; -webkit-transition: .5s left ease; } #navList li { float: left; margin: 0px,40px,0px,0px; -webkit-transition: 0.3s all ease; -moz-transition: 0.3s all ease; -ms-transition: 0.3s all ease; -o-transition: 0.3s all ease; transition: 0.3s all ease; overflow: hidden; position: relative; } #navList li:hover { background: #000; box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.4); } #navList li:hover .border { opacity: 1; left: 65px; } #navList li:hover a { color: #FFF; text-shadow: 1px 2px 4px #333; } #navList li:hover .menu { -webkit-animation-name: shake; -moz-animation-name: shake; } .menu { -webkit-animation: .5s .2s ease both; -moz-animation: 1s .2s ease both; } @-webkit-keyframes shake { 0%,100% { -webkit-transform: translateX(0); } 20%,60% { -webkit-transform: translateX(-10px); } 40%,80% { -webkit-transform: translateX(10px); } } @-moz-keyframes shake { 0%,100% { -moz-transform: translateX(0); } 20%,60% { -moz-transform: translateX(-10px); } 40%,80% { -moz-transform: translateX(10px); } } #navList a { text-decoration: none; display: block; width: 5em; height: 20px; float: left; text-align: center; font-weight: bold; padding-top: 8px; color: #515151; } .blogStats { float: right; font-style: italic; font-family: Georgia,'FZYaoTi',Microsoft YaHei; \5B8B\4F53, sans-serif; color: #757575; margin-right: 1px; text-align: right; } /*主頁文章列表*/ #main { width: 100%; text-align: left; margin-top: 10px; } #mainContent .forFlow { margin-left: 22em; float: none; width: auto; } #mainContent { min-height: 200px; padding: 0px 0px 10px 0; -o-text-overflow: ellipsis; text-overflow: ellipsis; overflow: hidden; word-break: break-all; float: left; margin-left: -22em; margin-top: 0; width: 100%; } /*日期*/ .day { text-decoration: none; background: #FFF; padding: 20px; margin-bottom: -1px; color: #515151; font-size: 21px; line-height: 1.5em; float: left; clear: right; } .day:hover { border: 1px solid #21759B; position: relative; z-index: 10; } .day:hover .postSeparator { border-top: 1px dashed #515151; } .dayTitle { text-decoration: none; } .dayTitle a { text-decoration: none; color: #515151; font-size: 13px; font-weight: bold; font-family: Georgia,Consolas,Microsoft YaHei, monospace; } /*文章標題*/ .postTitle { font-family: Georgia,'Consolas','FZYaoTi','STHeiti',Microsoft YaHei; \5B8B\4F53, sans-serif; font: 'Lucida Console',Georgia,'Microsoft YaHei',Microsoft YaHei; margin-bottom: 10px; font-size: 20px; font-weight: bold; float: right; width: 100%; clear: both; } .postTitle a:link, .postTitle a:visited, .postTitle a:active { color: #21759b; font-weight: bold; transition: all 0.4s linear 0s; } .postTitle a:hover { text-decoration: none; margin-left: 30px; font-weight: bold; color: #45bcf9; } .postTitle2 { text-decoration: none; font-size: 20px; font-weight: bold; font-family: Georgia,'Consolas','FZYaoTi','STHeiti',Microsoft YaHei; \5B8B\4F53, sans-serif; font: 'Lucida Console',Georgia,'Microsoft YaHei',Microsoft YaHei; padding-right: 64px; padding-left: 10px; border-left-style: solid; border-left-width: 3px; border-left-color: #515151; } .postCon { float: right; line-height: 1.5em; width: 100%; clear: both; padding: 10px 0; } .day .postTitle a { padding-left: 10px; } .postDesc { border-right: 3px solid #21759b; font-size: 12px; color: #21759b; float: right; width: 100%; clear: both; text-align: right; padding-left: 20px; padding-right: 5px; margin-top: 20px; line-height: 1.5; } .postDesc a:link, .postDesc a:visited, .postDesc a:active { color: #666; } .postDesc a:hover { color: #21759b; text-decoration: none; } .postSeparator { clear: both; height: 1px; width: 100%; clear: both; float: right; margin: 0 auto 15px auto; } /*側邊欄*/ #sideBar { margin-top: -15px; width: 240px; min-height: 200px; padding: 0px 0 0px 5px; float: right; -o-text-overflow: ellipsis; text-overflow: ellipsis; overflow: hidden; word-break: break-all; } #sideBar a { color: #757575; } #sideBar a:hover { color: #21759b; } .mySearch { background: #FFF; } .catListTitle { font-size: 16px; background-color: #169FE6; color: white; font-weight: normal; margin-bottom: 5px; } .catListEssay ul li { font-size: 12px; font-weight: normal; margin-left: -2.3em; } .liScore { font-family: Georgia,'Consolas','FZYaoTi','STHeiti',Microsoft YaHei; font-size: 12px; font-weight: normal; margin-left: -2.3em; } .liRank { font-family: Georgia,'Consolas','FZYaoTi','STHeiti',Microsoft YaHei; font-size: 12px; font-weight: normal; margin-left: -2.3em; } .catListTag { text-decoration: none; background: #FFF; margin-top: 10px; margin-bottom: 20px; } .catListTag ul { border-top: none; } .catListTag ul li { line-height: 44px; margin-left: -30px; color: #7e8c8d; } .catListPostArchive { background: #FFF; } .catListPostArchive ul { border-top: none; } .catListPostArchive ul li { line-height: 44px; color: #7e8c8d; margin-left: -30px; } .catListArticleCategory { width: 290px; padding-top: 20px; background: #FFF; margin-top: 20px; } .catListImageCategory { width: 290px; padding-top: 20px; background: #FFF; margin-top: 20px; } .catListComment { background: #FFF; margin-top: 20px; } #RecentCommentsBlock { padding: 10px; border: 1px solid #dedede; border-top: none; } .recent_comment_title { font-size: 15px; color: #7e8c8d; } .recent_comment_body, .recent_comment_author { border-bottom: 1px solid #E9E9E9; color: #9fa4a4; font-size: 13px; } .recent_comment_body { border-bottom: none; } .catListView { background: #FFF; margin-top: 20px; } #TopViewPostsBlock { padding: 10px; border: 1px solid #dedede; border-top: none; } .catListView ul li { border-bottom: 1px solid #E9E9E9; margin-left: -30px; margin-bottom: 5px; } .catListFeedback { background: #FFF; margin-top: 20px; } #TopFeedbackPostsBlock { padding: 10px; border-top: none; } .catListFeedback ul li { margin-left: -30px; } .catListLink { display: none; } .clearFix:after { clear: both; display: block; height: 0; line-height: 0; content: ""; visibility: hidden; } #myding { background: #99B16B; display: none; } #myadd:hover { opacity: 1; } #goto-top:hover { background: url(http://images.cnblogs.com/cnblogs_com/Li-Cheng/554829/o_goto-top.png) no-repeat 0 -36px; } /*日曆控制項樣式*/ #blog-calendar { float: center; width: 238px; margin-top: 20px; padding-bottom: 5px; margin-bottom: 20px; box-shadow: 0 1px 1px #ccc; } #blog-calendar td { font-size: 12px; font-family: "Comic Sans MS"; } #blog-calendar th { font-size: 12px; } #calendar { width: 238px; padding-bottom: 5px; margin-bottom: 35px; box-shadow: 0 1px 1px #ccc; } #calendar .Cal { width: 100%; line-height: 1.5em; } #calendar td { font-family: "Comic Sans MS"; background: #FFFFFF; padding-top: 2px; } .Cal { border: none; color: #666; text-decoration: none; } #calendar table a:hover { color: white; text-decoration: none; } .CalTodayDay { background: #FFF !important; text-decoration: none; } .CalWeekendDay { padding-top: 4px; padding-bottom: 4px; text-decoration: none; } .CalOtherMonthDay { color: #ccc; padding-top: 4px; padding-bottom: 4px; text-decoration: none; } #calendar .CalNextPrev a:link, #calendar .CalNextPrev a:visited, #calendar .CalNextPrev a:active { font-weig