toc.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <nav
  3. class="toc"
  4. :class="{ 'toc--fixed': fixed }"
  5. :style="{ 'top': offset + 'px' }">
  6. <ol class="group">
  7. <el-toc-item
  8. v-for="item in toc"
  9. class="title"
  10. :active="active"
  11. :item="item">
  12. </el-toc-item>
  13. </ol>
  14. </nav>
  15. </template>
  16. <script>
  17. import ElTocItem from './toc-item';
  18. import E from 'oui-dom-events';
  19. /**
  20. * 浮动在右边的目录
  21. */
  22. export default {
  23. name: 'toc',
  24. components: {
  25. ElTocItem
  26. },
  27. props: {
  28. main: {
  29. type: String,
  30. required: true
  31. }
  32. },
  33. data() {
  34. return {
  35. toc: [],
  36. offset: 0,
  37. fixed: false,
  38. active: 0
  39. };
  40. },
  41. events: {
  42. ['element.example.reload']() {
  43. this.$nextTick(() => {
  44. this.toc = [];
  45. this.anchors = this.findAllAnchors();
  46. this.scrollWrap = document.querySelector('body');
  47. this.currentHeaderHeight = document.querySelector('.app__main:first-of-type').offsetTop;
  48. this.fixedOffsetTop = document.querySelector('.app__menu__label').offsetTop;
  49. this.updateFixed();
  50. this.generateTOC(this.anchors, this.toc);
  51. E.off(this.scrollWrap, 'mousewheel.toc.activecurrent');
  52. this.scrollActiveCurrent();
  53. });
  54. }
  55. },
  56. ready() {
  57. this.scrollAutoFixed();
  58. },
  59. methods: {
  60. /**
  61. * @see https://github.com/chjj/marked/issues/334
  62. */
  63. generateTOC(anchors, toc) {
  64. if (document.querySelector('.no-toc')) {
  65. return;
  66. }
  67. let cache = {};
  68. for (let key in anchors) {
  69. const current = this.fetchAnchorData(anchors[key]);
  70. const level = current.level || 1;
  71. const last = cache[level - 1];
  72. current.id = Number(key);
  73. if (last) {
  74. last.children = (last.children || []).concat(current);
  75. } else {
  76. cache[level] = current;
  77. toc.push(current);
  78. }
  79. }
  80. },
  81. fetchAnchorData(anchor) {
  82. const head = anchor.parentNode;
  83. const level = head.tagName.match(/\d/g)[0];
  84. const heading = head.getAttribute('id');
  85. return { level, heading };
  86. },
  87. updateFixed() {
  88. if (this.scrollWrap.scrollTop <= this.fixedOffsetTop / 2) {
  89. this.offset = this.currentHeaderHeight;
  90. this.fixed = false;
  91. return;
  92. }
  93. if (this.fixed) return;
  94. this.offset = this.fixedOffsetTop;
  95. this.fixed = true;
  96. },
  97. scrollAutoFixed() {
  98. E.on(document, 'mousewheel.toc.autofixed', this.updateFixed);
  99. },
  100. findAllAnchors() {
  101. return Array.prototype.slice.call(document.querySelectorAll('.anchor'));
  102. },
  103. scrollActiveCurrent() {
  104. const anchors = this.findAllAnchors();
  105. const body = this.scrollWrap;
  106. const distances = [];
  107. let current;
  108. let index = 0;
  109. let max;
  110. for (let anchor of anchors) {
  111. distances.push(anchor.offsetTop);
  112. }
  113. current = distances[index];
  114. max = distances.length - 1;
  115. this.active = index;
  116. // 记录当前滚动档位
  117. // 判断当前滚动和下个档位
  118. E.on(this.scrollWrap, 'mousewheel.toc.activecurrent', e => {
  119. const scrollTop = body.scrollTop;
  120. while (index < max && scrollTop > current) {
  121. current = distances[++index];
  122. this.active = index;
  123. }
  124. while (index && scrollTop < current) {
  125. current = distances[--index];
  126. this.active = index;
  127. }
  128. });
  129. }
  130. },
  131. beforeDestroy() {
  132. E.off(this.scrollWrap, 'mousewheel.toc.autofixed');
  133. E.off(this.scrollWrap, 'mousewheel.toc.activecurrent');
  134. }
  135. };
  136. </script>
  137. <style lang="css">
  138. .toc {
  139. color: #999;
  140. margin-right: 40px;
  141. margin-top: -14px;
  142. position: absolute;
  143. right: 0;
  144. top: 0;
  145. width: 160px;
  146. &.toc--fixed {
  147. position: fixed;
  148. }
  149. .group {
  150. font-size: 13px;
  151. list-style-type: none;
  152. margin: 0;
  153. padding-left: 12px;
  154. }
  155. .item {
  156. margin: 14px 0;
  157. .toc__link:hover,
  158. .toc__link.active {
  159. color: #216fc1;
  160. }
  161. .group {
  162. border-left: 1px solid rgba(31, 109, 191, .5);
  163. }
  164. }
  165. .title {
  166. margin-bottom: 18px;
  167. }
  168. }
  169. </style>