爲本身的博客園添加目錄錨點和返回頂部

 

 

下面的代碼能夠在博客園的「設置」裏經過文件的方式引入。javascript

方法一:

適合不會改代碼的小白,以及不須要作個性化修改的朋友。css

優勢:只用操做1步html

缺點:不能自定義錨點的範圍(設置的基本能夠通用),以及個性化定製java

頁首引入

  1. <linktype="text/css"rel="stylesheet"href="http://files.cnblogs.com/files/miangao/maodian.css">
  2. <scriptsrc="http://files.cnblogs.com/files/miangao/maodian.js"></script>
  3. <scriptsrc="http://files.cnblogs.com/files/miangao/bootstrap.min.js"></script>

 

例:bootstrap

 

 

方法二:

適合會修改JS代碼的朋友(可選擇作個性化修改),比方法一多操做兩步。app

 

文件引入的方式不方便修改,因而我這裏寫在「設置」裏;ide

 

若是你沒有獲取博客園JS的權限,那須要去提交申請獲取。post

個人一天時間就申請下來了,申請好了是這個樣子測試

 

 

添加自動目錄的基本原理this

大概的原理就是遍歷整個內容,找到其中的標題標籤,h二、h3,而後把這些標題插入到一個容器中,針對這個容器設置絕對定位的css便可。主要代碼以下:

博客側邊欄廣告內引入

 *通過我屢次測試發現,爲啥直接點複製代碼,粘貼過去的代碼就要報錯,手動選擇複製過去的代碼就不會。

複製代碼
  1. <scripttype="text/javascript">
  2. //如下是錨點JS
  3. var a = $(document);
  4. a.ready(function(){
  5. var b = $('body'),
  6. d ='sideToolbar',
  7. e ='sideCatalog',
  8. f ='sideCatalog-catalog',
  9. g ='sideCatalogBtn',
  10. h ='sideToolbar-up',
  11. i ='<div id="sideToolbar"style="display:none;">\<div class="sideCatalogBg"id="sideCatalog">\<div id="sideCatalog-sidebar">\<div class="sideCatalog-sidebar-top"></div>\<div class="sideCatalog-sidebar-bottom"></div>\</div>\<div id="sideCatalog-catalog">\<ul class="nav"style="width:225px;zoom:1">\</ul>\</div>\</div>\<a href="javascript:void(0);"id="sideCatalogBtn"class="sideCatalogBtnDisable"></a>\</div>',
  12. j ='',
  13. k =200,
  14. l =0,
  15. m =0,
  16. n =0,
  17. //限制存在個數,如數量過多,則只顯示h2,不顯示h3
  18. //o, p = 13,
  19. o, p =100,
  20. q =true,
  21. r =true,
  22. s = b;
  23. if(s.length ===0){
  24. return
  25. };
  26. b.append(i);
  27. //指定獲取目錄的範圍-------------這一點很是重要,由於每一個人指定的範圍都不同,因此這是要修改的地方
  28. //o = s.find(':header');
  29. o = $('#cnblogs_post_body').find(':header')
  30. if(o.length > p){
  31. r =false;
  32. var t = s.find('h2');
  33. var u = s.find('h3');
  34. if(t.length + u.length > p){
  35. q =false
  36. }
  37. };
  38. o.each(function(t){
  39. var u = $(this),
  40. v = u[0];
  41. var title = u.text();
  42. var text = u.text();
  43. u.attr('id','autoid-'+ l +'-'+ m +'-'+ n)
  44. //if (!u.attr('id')) {
  45. // u.attr('id', 'autoid-' + l + '-' + m + '-' + n)
  46. //};
  47. if(v.localName ==='h2'){
  48. l++;
  49. m =0;
  50. if(text.length >14) text = text.substr(0,20)+"...";
  51. j +='<li><span>'+ l +'&nbsp&nbsp</span><a href="#'+ u.attr('id')+'" title="'+ title +'">'+ text +'</a><span class="sideCatalog-dot"></span></li>';
  52. }elseif(v.localName ==='h3'){
  53. m++;
  54. n =0;
  55. if(q){
  56. if(text.length >12) text = text.substr(0,16)+"...";
  57. j +='<li class="h2Offset"><span>'+ l +'.'+ m +'&nbsp&nbsp</span><a href="#'+ u.attr('id')+'" title="'+ title +'">'+ text +'</a></li>';
  58. }
  59. }elseif(v.localName ==='h4'){
  60. n++;
  61. if(r){
  62. j +='<li class="h3Offset"><span>'+ l +'.'+ m +'.'+ n +'&nbsp&nbsp</span><a href="#'+ u.attr('id')+'" title="'+ title +'">'+ u.text()+'</a></li>';
  63. }
  64. }
  65. });
  66. $('#'+ f +'>ul').html(j);
  67. b.data('spy','scroll');
  68. b.data('target','.sideCatalogBg');
  69. $('body').scrollspy({
  70. target:'.sideCatalogBg'
  71. });
  72. $sideCatelog = $('#'+ e);
  73. $('#'+ g).on('click',function(){
  74. if($(this).hasClass('sideCatalogBtnDisable')){
  75. $sideCatelog.css('visibility','hidden')
  76. }else{
  77. $sideCatelog.css('visibility','visible')
  78. };
  79. $(this).toggleClass('sideCatalogBtnDisable')
  80. });
  81. $('#'+ h).on('click',function(){
  82. $("html,body").animate({
  83. scrollTop:0
  84. },500)
  85. });
  86. $sideToolbar = $('#'+ d);
  87. //經過判斷評論框是否存在顯示索引目錄
  88. var commentDiv = $("#blog-comments-placeholder");
  89. a.on('scroll',function(){
  90. //評論框存在才調用方法
  91. if(commentDiv.length >0){
  92. var t = a.scrollTop();
  93. if(t > k){
  94. $sideToolbar.css('display','block');
  95. $('#gotop').show()
  96. }else{
  97. $sideToolbar.css('display','none')
  98. $('#gotop').hide()
  99. }
  100. }
  101. })
  102. });
  103. //以上是錨點JS
  104. //如下是返回頂部JS
  105. $(function(){
  106. $('body').append('<div id="gotop" onclick="goTop();"></div>');
  107. });
  108. function goTop(u, t, r){
  109. var scrollActivate =!0;
  110. if(scrollActivate){
  111. u = u ||0.1;
  112. t = t ||16;
  113. var s =0,
  114. q =0,
  115. o =0,
  116. p =0,
  117. n =0,
  118. j =0;
  119. document.documentElement &&(s = document.documentElement.scrollLeft ||0, q = document.documentElement.scrollTop ||0);
  120. document.body &&(o = document.body.scrollLeft ||0, p = document.body.scrollTop ||0);
  121. n = window.scrollX ||0;
  122. j = window.scrollY ||0;
  123. s =Math.max(s,Math.max(o, n));
  124. q =Math.max(q,Math.max(p, j));
  125. p =1+ u;
  126. window.scrollTo(Math.floor(s / p),Math.floor(q / p));
  127. 0< s ||0< q ? window.setTimeout('goTop('+ u +', '+ t +')', t):'undefined'!=typeof r && r()
  128. }else{
  129. scrollActivate =!0
  130. }
  131. }
  132. //以上是返回頂部JS
  133. </script>
複製代碼

 

其中須要怎麼定製,就看朋友們本身修改JS代碼了

頁首引入

  1. <linktype="text/css"rel="stylesheet"href="http://files.cnblogs.com/files/miangao/maodian.css">

頁尾引入

  1. <scriptsrc="http://files.cnblogs.com/files/miangao/bootstrap.min.js"></script>

 

好了,這樣就完成了,是否是很超級簡單。

 

但願能給你們帶來一點便利,謝謝!

相關文章
相關標籤/搜索