anchor_list.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //是否显示导航栏
  2. var showNavBar = false;
  3. //是否展开导航栏
  4. var expandNavBar = true;
  5. var currentIndex = 0;
  6. var currentScrollHigh = 0;
  7. var currentContentScrollHigh = 0;
  8. /*function sleep(numberMillis) {
  9. var now = new Date();
  10. var exitTime = now.getTime() + numberMillis;
  11. while (true) {
  12. now = new Date();
  13. if (now.getTime() > exitTime)
  14. return;
  15. }
  16. }*/
  17. /*$(window).onbeforeunload = function(){
  18. currentIndex = 0;
  19. } */
  20. $(window).load(function(){
  21. var h1s = $("body").find("h1");
  22. var h2s = $("body").find("h2");
  23. var h3s = $("body").find("h3");
  24. var h4s = $("body").find("h4");
  25. var h5s = $("body").find("h5");
  26. var h6s = $("body").find("h6");
  27. var headCounts = [h1s.length, h2s.length, h3s.length, h4s.length, h5s.length, h6s.length];
  28. var vH1Tag = null; // 显示的最高层级
  29. var vH2Tag = null; // 显示的最低层级
  30. var sum = 0;
  31. for(var i = 0; i < headCounts.length; i++){
  32. if(headCounts[i] > 0){
  33. for( var y = 0; y < headCounts[i]; y++)
  34. sum = sum + 1;
  35. }
  36. }
  37. for(var i = 0; i < headCounts.length; i++){
  38. if(headCounts[i] > 0){
  39. if(vH1Tag == null){
  40. vH1Tag = 'h' + (i + 1);
  41. }else{
  42. vH2Tag = 'h' + (i + 1);
  43. }
  44. }
  45. }
  46. if(vH1Tag == null){
  47. return;
  48. }
  49. $("#page-wrapper").after('<div class="BlogAnchor">' +
  50. //'<span style="color:red;position:absolute;top:-6px;left:0px;cursor:pointer;" οnclick="$(\'.BlogAnchor\').hide();">×</span>' +
  51. //'<p>' +
  52. // '<b id="AnchorContentToggle" title="收起" style="cursor:pointer;">目录▲</b>' +
  53. //'</p>' +
  54. '<div class="AnchorContent" id="AnchorContent"> </div>' +
  55. '</div>' );
  56. var vH1Index = 0;
  57. var vH2Index = 0;
  58. var vIndexH1 = 0;
  59. var vIndexH2 = 0;
  60. var vIndexH3 = 0;
  61. var vIndexH4 = 0;
  62. var vIndexH5 = 0;
  63. var vIndexH6 = 0;
  64. var headerALL = [];
  65. var headerIDALL = [];
  66. var headerHightALL = [];
  67. $("body").find("h1,h2,h3,h4,h5,h6").each(function(i,item){
  68. var id = '';
  69. var name = '';
  70. var tag = $(item).get(0).tagName.toLowerCase();
  71. var className = '';
  72. // i=0 tag=h1 i=1 tag=h2 i=2 tag=h2
  73. if(tag == "h1"){
  74. id = name = ++vIndexH1;
  75. vIndexH2 = 0;
  76. vIndexH3 = 0;
  77. vIndexH4 = 0;
  78. vIndexH5 = 0;
  79. vIndexH6 = 0;
  80. className = 'item_h1';
  81. //alert("tag ="+ tag +"---- i = "+ i + " name ="+ name +" className= "+ className);
  82. //tag =h1---- i = 0 name =1 className= item_h1
  83. }else if(tag == "h2"){
  84. id = vIndexH1 + '_' + ++vIndexH2;
  85. name = vIndexH1 + '.' + vIndexH2;
  86. className = 'item_h2';
  87. vIndexH3 = 0;
  88. vIndexH4 = 0;
  89. vIndexH5 = 0;
  90. vIndexH6 = 0;
  91. }else if(tag == "h3"){
  92. id = vIndexH1 + '_' + vIndexH2+ '_' + ++vIndexH3;
  93. name = vIndexH1 + '.' + vIndexH2+ '.' + +vIndexH3;
  94. className = 'item_h3';
  95. vIndexH4 = 0;
  96. vIndexH5 = 0;
  97. vIndexH6 = 0;
  98. }
  99. else if(tag == "h4"){
  100. id = vIndexH1 + '_' + vIndexH2+ '_' +vIndexH3 +'_'+ ++vIndexH4 ;
  101. name = vIndexH1 + '.' + vIndexH2+ '.' +vIndexH3 +'.'+ vIndexH4 ;
  102. className = 'item_h4';
  103. vIndexH5 = 0;
  104. vIndexH6 = 0;
  105. }
  106. else if(tag == "h5"){
  107. id = vIndexH1 + '_' + vIndexH2+ '_' +vIndexH3 +'_' +vIndexH4+'_'+ ++vIndexH5;
  108. name = vIndexH1 + '.' + vIndexH2+ '.' +vIndexH3 +'.' +vIndexH4+'.'+ vIndexH5;
  109. className = 'item_h5';
  110. vIndexH6 = 0;
  111. }
  112. else if(tag == "h6"){
  113. id = vIndexH1 + '_' + vIndexH2+ '_' +vIndexH3 +'_' +vIndexH4+'_' +vIndexH5+'_'+ ++vIndexH6;
  114. name = vIndexH1 + '.' + vIndexH2+ '.' +vIndexH3 +'.' +vIndexH4+'.' +vIndexH5+'.'+ vIndexH6;
  115. className = 'item_h6';
  116. }
  117. if(name.length > 2){
  118. var mFirstName = name.substring(0,2);
  119. while(mFirstName == "0."){
  120. name = name.substring(2,name.length);
  121. mFirstName = name.substring(0,2);
  122. }
  123. }
  124. $(item).attr("id","wow"+id+"_index_"+i);
  125. $(item).addClass("wow_head");
  126. var itemHeight = $(item).offset().top
  127. $("#AnchorContent").css('max-height', ($(document).height()) + 'px');
  128. $("#AnchorContent").css('height', ($(window).height()) + 'px');
  129. $("#AnchorContent").css('overflow','auto');
  130. $("#AnchorContent").append('<li><a class="nav_item '+className+' anchor-link" href="#wow'+id+'_index_'+i+'" link="#wow'+id+'" index="'+i+'">'+name+" "+$(this).text()+" "+'</a></li>');
  131. var str = "#wow"+id+"_index_"+i;
  132. headerALL.push($(item));
  133. headerIDALL.push(str);
  134. console.log(" i = "+ i +" id =" + id +" itemHeight = "+ itemHeight);
  135. });
  136. // 1
  137. // 1.1
  138. // 1.1.1
  139. // 1.1.1.1
  140. // 1.1.1.1.1
  141. // 1.1.1.1.1.1
  142. $("#AnchorContentToggle").click(function(){
  143. var state = $(this).attr("state");
  144. if(state=="show"){
  145. //$(this).html("目录▼");
  146. $(this).attr({"state":"hide"});
  147. $("#page-wrapper").css("marginRight",'0');
  148. //$("#page-wrapper").css("max-width","unset");
  149. $(".next").css("right",'0');
  150. $(".BlogAnchor").css("transform","translateX(var(--sidebar-width))");
  151. }else{
  152. //$(this).html("目录▲");
  153. $(this).attr({"state":"show"});
  154. $("#page-wrapper").css("marginRight","var(--sidebar-width)");
  155. //$("#page-wrapper").css("max-width",xlength);
  156. $(".next").css("right","var(--sidebar-width)");
  157. $(".BlogAnchor").css("transform","translateX(0)");
  158. }
  159. $("#AnchorContent").toggle();
  160. });
  161. $(".anchor-link").click(function(){
  162. //$("html,body").animate({scrollTop: $($(this).attr("link")).offset().top}, 10);
  163. var index = $(this).attr("index");
  164. //$(".BlogAnchor li .nav_item.current").removeClass('current');
  165. //$(headerNavs[index]).addClass('current');
  166. var scrollTop = $(window).scrollTop(); // 获得将要到达的点离顶距离
  167. currentScrollHigh = scrollTop;
  168. currentContentScrollHigh = headerHightALL[index];
  169. var value = headerTops[index];
  170. currentIndex = value;
  171. console.log("index = "+ index+ " headerTops["+index+"] ="+ value + "scrollTop="+ scrollTop);
  172. });
  173. var headerNavs = $(".BlogAnchor li .nav_item");
  174. var headerTops = [];
  175. var mHeight = 0;
  176. $(".wow_head").each(function(i, n){
  177. var value = $(n).offset().top;
  178. headerTops.push($(n).offset().top);
  179. console.log("i = "+ i+ " offset().top ="+ value);
  180. });
  181. headerTops.push($(document).height());
  182. window.onresize = function(){
  183. headerTops = [];
  184. $.each(headerNavs, function(i, n){
  185. $(n).trigger("click");
  186. //document.querySelector(headerIDALL[i]).scrollIntoView(true);
  187. //var high = $(n).offset().top;
  188. var scrollTop = $(window).scrollTop();
  189. headerTops.push(scrollTop);
  190. console.log("headerNavs_index="+i +" scrollTop="+scrollTop +" headerTops="+headerTops[i]);
  191. }
  192. );
  193. headerTops.push($(document).height());
  194. //$("#AnchorContent").height($(window).height());
  195. $("#AnchorContent").css('height', ($(window).height()) + 'px');
  196. var xcontentWidth = $("#AnchorContent").width();
  197. var xWidth = $(window).width();
  198. var xlength = xWidth - xcontentWidth;
  199. //$("body").css("marginRight",var(--sidebar-width));
  200. $(".next").css("right","var(--sidebar-width)");
  201. //$("body").css("max-width",xlength);
  202. //$(".BlogAnchor").css("top",headbar_height+'px')
  203. $(headerNavs[currentIndex]).trigger("click");
  204. //document.querySelector(headerIDALL[i]).scrollIntoView(true);
  205. }
  206. $(window).scroll(function(){
  207. var scrollTop = $(window).scrollTop(); // 获得将要到达的点离顶距离
  208. $.each(headerTops, function(i, n){
  209. if( (scrollTop >= (headerTops[i]) && scrollTop < (headerTops[i+1] -1)) ){
  210. console.log("headerTops[i-1]"+headerTops[i-1]+"headerTops[i]"+headerTops[i]+" scrollTop ="+ scrollTop+" headerTops[i+1]= "+headerTops[i+1] +" i ="+i);
  211. //$(".BlogAnchor li .nav_item.current").removeClass('current');
  212. //$(headerNavs[i]).addClass('current');
  213. currentScrollHigh = scrollTop;
  214. currentContentScrollHigh = headerHightALL[i];
  215. currentIndex = i;
  216. var mxxHeight = $("#AnchorContent").height()
  217. var mscrollTop1 = $("#AnchorContent").scrollTop(); // 当前标签的高度
  218. console.log("zukgit2 currentIndex="+ currentIndex );
  219. console.log("zukgit2 windows.scrollTop="+ scrollTop );
  220. console.log("zukgit2 $(window).height()="+ $(window).height() );
  221. console.log("zukgit2 currentContentScrollHigh="+ currentContentScrollHigh );
  222. console.log("zukgit2 AnchorContent.mscrollTop="+ mscrollTop1 );
  223. console.log("zukgit2 AnchorContent.high="+ mxxHeight );
  224. if((currentContentScrollHigh - mscrollTop1)> ($(window).height()/2)){ //↓ // 如果当前index超出 界面的高度
  225. //var sum = Math.floor(headerHightALL(i) / $(window).height());
  226. var dif = currentContentScrollHigh - mscrollTop1;
  227. $("#AnchorContent").animate({scrollTop: (currentContentScrollHigh)}, 50);
  228. console.log(" i(zukgit3) = "+ i +" currentContentScrollHigh =" + currentContentScrollHigh +" mscrollTop1 = "+ mscrollTop1);
  229. console.log(" $(window).height()="+ $(window).height() );
  230. } else if( ( mscrollTop1 - currentContentScrollHigh )> 0 ){ //↑
  231. $("#AnchorContent").animate({scrollTop: currentContentScrollHigh-($("#AnchorContent").height()/3)}, 50);
  232. console.log(" i(zukgit4) = "+ i +" currentContentScrollHigh =" + currentContentScrollHigh +" mscrollTop1 = "+ mscrollTop1);
  233. console.log(" $(window).height()="+ $(window).height() +" $(#AnchorContent).height()="+mxxHeight);
  234. }
  235. return false;
  236. }
  237. }
  238. );
  239. if(scrollTop == 0){
  240. $("#AnchorContent").animate({scrollTop: 0}, 50);
  241. }
  242. if(scrollTop == $(document).height()){
  243. $("#AnchorContent").animate({scrollTop: headerHightALL[headerHightALL.length-1]}, 50);
  244. }
  245. });
  246. $.each(headerNavs, function(i, n){
  247. var high = $(n).offset().top;
  248. headerHightALL.push(high);
  249. console.log("high"+high);
  250. }
  251. );
  252. headerTops = [];
  253. $.each(headerNavs, function(i, n){
  254. $(n).trigger("click");
  255. //document.querySelector(headerIDALL[i]).scrollIntoView(true);
  256. var scrollTop = $(window).scrollTop();
  257. headerTops.push(scrollTop);
  258. console.log("headerNavs_index="+i +" scrollTop="+scrollTop +" headerTops="+headerTops[i]);
  259. }
  260. );
  261. headerTops.push($(document).height());
  262. $(headerNavs[0]).trigger("click");
  263. //document.querySelector(headerIDALL[0]).scrollIntoView(true);
  264. var xcontentWidth = $("#AnchorContent").width();
  265. var headbar_height = $("#menu-bar-hover-placeholder").height();
  266. var xWidth = $(window).width();
  267. var xlength = xWidth - xcontentWidth;
  268. //$("#page-wrapper").css("marginRight","var(--sidebar-width)");
  269. //$("#page-wrapper").css("max-width",xlength);
  270. $(".next").css("right","var(--sidebar-width)");
  271. //$(".BlogAnchor").css("top",headbar_height+'px')
  272. if(!showNavBar){
  273. //$('.BlogAnchor').hide();
  274. $("#AnchorContentToggle").attr({"state":"show"});
  275. $("#page-wrapper").css("marginRight","var(--sidebar-width)");
  276. //$("#page-wrapper").css("max-width",xlength);
  277. $(".next").css("right","var(--sidebar-width)");
  278. $(".BlogAnchor").css("transform","translateX(0)");
  279. }
  280. if(!expandNavBar){
  281. //$(this).html("目录▼");
  282. //$(this).attr({"title":"展开"});
  283. $("#AnchorContent").hide();
  284. }
  285. });