anchor_list.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //是否显示导航栏
  2. var showNavBar = true;
  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("transform","translateX(0)");
  148. //$(".next").css("transform","translateX(0)");
  149. //$("#page-wrapper").removeClass('anchor-visible')
  150. //$("#page-wrapper").css("max-width","unset");
  151. $(".BlogAnchor").css("transform","translateX(var(--sidebar-width))");
  152. }else{
  153. //$(this).html("目录▲");
  154. $(this).attr({"state":"show"});
  155. $("#page-wrapper").css("transform","translateX(var(--anchorbar-width))");
  156. //$(".next").css("transform","translateX(var(--anchorbar-width))");
  157. //$("#page-wrapper").addClass('anchor-visible')
  158. //$("#page-wrapper").css("max-width",xlength);
  159. $(".BlogAnchor").css("transform","translateX(0)");
  160. }
  161. $("#AnchorContent").toggle();
  162. });
  163. $(".anchor-link").click(function(){
  164. //$("html,body").animate({scrollTop: $($(this).attr("link")).offset().top}, 10);
  165. var index = $(this).attr("index");
  166. //$(".BlogAnchor li .nav_item.current").removeClass('current');
  167. //$(headerNavs[index]).addClass('current');
  168. var scrollTop = $(window).scrollTop(); // 获得将要到达的点离顶距离
  169. currentScrollHigh = scrollTop;
  170. currentContentScrollHigh = headerHightALL[index];
  171. var value = headerTops[index];
  172. currentIndex = value;
  173. console.log("index = "+ index+ " headerTops["+index+"] ="+ value + "scrollTop="+ scrollTop);
  174. });
  175. var headerNavs = $(".BlogAnchor li .nav_item");
  176. var headerTops = [];
  177. var mHeight = 0;
  178. $(".wow_head").each(function(i, n){
  179. var value = $(n).offset().top;
  180. headerTops.push($(n).offset().top);
  181. console.log("i = "+ i+ " offset().top ="+ value);
  182. });
  183. headerTops.push($(document).height());
  184. window.onresize = function(){
  185. headerTops = [];
  186. $.each(headerNavs, function(i, n){
  187. $(n).trigger("click");
  188. //document.querySelector(headerIDALL[i]).scrollIntoView(true);
  189. //var high = $(n).offset().top;
  190. var scrollTop = $(window).scrollTop();
  191. headerTops.push(scrollTop);
  192. console.log("headerNavs_index="+i +" scrollTop="+scrollTop +" headerTops="+headerTops[i]);
  193. }
  194. );
  195. headerTops.push($(document).height());
  196. //$("#AnchorContent").height($(window).height());
  197. $("#AnchorContent").css('height', ($(window).height()) + 'px');
  198. var xcontentWidth = $("#AnchorContent").width();
  199. var xWidth = $(window).width();
  200. var xlength = xWidth - xcontentWidth;
  201. //$("body").css("marginRight",var(--sidebar-width));
  202. $(".next").css("right","var(--sidebar-width)");
  203. //$("body").css("max-width",xlength);
  204. //$(".BlogAnchor").css("top",headbar_height+'px')
  205. $(headerNavs[currentIndex]).trigger("click");
  206. //document.querySelector(headerIDALL[i]).scrollIntoView(true);
  207. }
  208. $(window).scroll(function(){
  209. var scrollTop = $(window).scrollTop(); // 获得将要到达的点离顶距离
  210. $.each(headerTops, function(i, n){
  211. if( (scrollTop >= (headerTops[i]) && scrollTop < (headerTops[i+1] -1)) ){
  212. console.log("headerTops[i-1]"+headerTops[i-1]+"headerTops[i]"+headerTops[i]+" scrollTop ="+ scrollTop+" headerTops[i+1]= "+headerTops[i+1] +" i ="+i);
  213. //$(".BlogAnchor li .nav_item.current").removeClass('current');
  214. //$(headerNavs[i]).addClass('current');
  215. currentScrollHigh = scrollTop;
  216. currentContentScrollHigh = headerHightALL[i];
  217. currentIndex = i;
  218. var mxxHeight = $("#AnchorContent").height()
  219. var mscrollTop1 = $("#AnchorContent").scrollTop(); // 当前标签的高度
  220. console.log("zukgit2 currentIndex="+ currentIndex );
  221. console.log("zukgit2 windows.scrollTop="+ scrollTop );
  222. console.log("zukgit2 $(window).height()="+ $(window).height() );
  223. console.log("zukgit2 currentContentScrollHigh="+ currentContentScrollHigh );
  224. console.log("zukgit2 AnchorContent.mscrollTop="+ mscrollTop1 );
  225. console.log("zukgit2 AnchorContent.high="+ mxxHeight );
  226. if((currentContentScrollHigh - mscrollTop1)> ($(window).height()/2)){ //↓ // 如果当前index超出 界面的高度
  227. //var sum = Math.floor(headerHightALL(i) / $(window).height());
  228. var dif = currentContentScrollHigh - mscrollTop1;
  229. $("#AnchorContent").animate({scrollTop: (currentContentScrollHigh)}, 50);
  230. console.log(" i(zukgit3) = "+ i +" currentContentScrollHigh =" + currentContentScrollHigh +" mscrollTop1 = "+ mscrollTop1);
  231. console.log(" $(window).height()="+ $(window).height() );
  232. } else if( ( mscrollTop1 - currentContentScrollHigh )> 0 ){ //↑
  233. $("#AnchorContent").animate({scrollTop: currentContentScrollHigh-($("#AnchorContent").height()/3)}, 50);
  234. console.log(" i(zukgit4) = "+ i +" currentContentScrollHigh =" + currentContentScrollHigh +" mscrollTop1 = "+ mscrollTop1);
  235. console.log(" $(window).height()="+ $(window).height() +" $(#AnchorContent).height()="+mxxHeight);
  236. }
  237. return false;
  238. }
  239. }
  240. );
  241. if(scrollTop == 0){
  242. $("#AnchorContent").animate({scrollTop: 0}, 50);
  243. }
  244. if(scrollTop == $(document).height()){
  245. $("#AnchorContent").animate({scrollTop: headerHightALL[headerHightALL.length-1]}, 50);
  246. }
  247. });
  248. $.each(headerNavs, function(i, n){
  249. var high = $(n).offset().top;
  250. headerHightALL.push(high);
  251. console.log("high"+high);
  252. }
  253. );
  254. headerTops = [];
  255. $.each(headerNavs, function(i, n){
  256. $(n).trigger("click");
  257. //document.querySelector(headerIDALL[i]).scrollIntoView(true);
  258. var scrollTop = $(window).scrollTop();
  259. headerTops.push(scrollTop);
  260. console.log("headerNavs_index="+i +" scrollTop="+scrollTop +" headerTops="+headerTops[i]);
  261. }
  262. );
  263. headerTops.push($(document).height());
  264. $(headerNavs[0]).trigger("click");
  265. //document.querySelector(headerIDALL[0]).scrollIntoView(true);
  266. var xcontentWidth = $("#AnchorContent").width();
  267. var headbar_height = $("#menu-bar-hover-placeholder").height();
  268. var xWidth = $(window).width();
  269. var xlength = xWidth - xcontentWidth;
  270. //$("#page-wrapper").css("transform","translateX(var(--sidebar-width))");
  271. $("#page-wrapper").css("width","100vw");
  272. //$(".next").css("transform","translateX(var(--sidebar-width))");
  273. $(".BlogAnchor").css("transform","translateX(var(--sidebar-width))")
  274. if(!showNavBar){
  275. //$('.BlogAnchor').hide();
  276. $("#AnchorContentToggle").attr({"state":"hide"});
  277. $("#page-wrapper").css(css("transform","translateX(0)"));
  278. //$("#page-wrapper").css("max-width",xlength);
  279. $(".next").css(css("transform","translateX(0)"));
  280. $(".BlogAnchor").css("transform","translateX(0)");
  281. }
  282. if(!expandNavBar){
  283. //$(this).html("目录▼");
  284. //$(this).attr({"title":"展开"});
  285. //$("#AnchorContent").hide();
  286. }
  287. });