component.tpl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <style>
  2. .page-component__scroll {
  3. height: calc(100% - 80px);
  4. margin-top: 80px;
  5. > .el-scrollbar__wrap {
  6. overflow-x: auto;
  7. }
  8. }
  9. .page-component {
  10. box-sizing: border-box;
  11. height: 100%;
  12. &.page-container {
  13. padding: 0;
  14. }
  15. .page-component__nav {
  16. width: 240px;
  17. position: fixed;
  18. top: 0;
  19. bottom: 0;
  20. margin-top: 80px;
  21. transition: padding-top .3s;
  22. > .el-scrollbar__wrap {
  23. height: 100%;
  24. overflow-x: auto;
  25. }
  26. &.is-extended {
  27. padding-top: 0;
  28. }
  29. }
  30. .side-nav {
  31. height: 100%;
  32. padding-top: 50px;
  33. padding-bottom: 50px;
  34. padding-right: 0;
  35. & > ul {
  36. padding-bottom: 50px;
  37. }
  38. }
  39. .page-component__content {
  40. padding-left: 270px;
  41. padding-bottom: 100px;
  42. box-sizing: border-box;
  43. }
  44. .content {
  45. padding-top: 50px;
  46. > {
  47. h3 {
  48. margin: 55px 0 20px;
  49. }
  50. table {
  51. border-collapse: collapse;
  52. width: 100%;
  53. background-color: #fff;
  54. font-size: 14px;
  55. margin-bottom: 45px;
  56. line-height: 1.5em;
  57. strong {
  58. font-weight: normal;
  59. }
  60. td, th {
  61. border-bottom: 1px solid #dcdfe6;
  62. padding: 15px;
  63. max-width: 250px;
  64. }
  65. th {
  66. text-align: left;
  67. white-space: nowrap;
  68. color: #909399;
  69. font-weight: normal;
  70. }
  71. td {
  72. color: #606266;
  73. }
  74. th:first-child, td:first-child {
  75. padding-left: 10px;
  76. }
  77. }
  78. ul:not(.timeline) {
  79. margin: 10px 0;
  80. padding: 0 0 0 20px;
  81. font-size: 14px;
  82. color: #5e6d82;
  83. line-height: 2em;
  84. }
  85. }
  86. }
  87. }
  88. @media (max-width: 768px) {
  89. .page-component {
  90. .page-component__nav {
  91. width: 100%;
  92. position: static;
  93. margin-top: 0;
  94. }
  95. .side-nav {
  96. padding-top: 0;
  97. padding-left: 50px;
  98. }
  99. .page-component__content {
  100. padding-left: 10px;
  101. padding-right: 10px;
  102. }
  103. .content {
  104. padding-top: 0;
  105. }
  106. .content > table {
  107. overflow: auto;
  108. display: block;
  109. }
  110. }
  111. }
  112. </style>
  113. <template>
  114. <el-scrollbar class="page-component__scroll" ref="componentScrollBar">
  115. <div class="page-container page-component">
  116. <el-scrollbar class="page-component__nav">
  117. <side-nav :data="navsData[lang]" :base="`/${ lang }/component`"></side-nav>
  118. </el-scrollbar>
  119. <div class="page-component__content">
  120. <router-view class="content"></router-view>
  121. <footer-nav></footer-nav>
  122. </div>
  123. <el-backtop
  124. v-if="showBackToTop"
  125. target=".page-component__scroll .el-scrollbar__wrap"
  126. :right="100"
  127. :bottom="150"
  128. ></el-backtop>
  129. </div>
  130. </el-scrollbar>
  131. </template>
  132. <script>
  133. import bus from '../../bus';
  134. import navsData from '../../nav.config.json';
  135. import throttle from 'throttle-debounce/throttle';
  136. export default {
  137. data() {
  138. return {
  139. lang: this.$route.meta.lang,
  140. navsData,
  141. scrollTop: 0,
  142. showHeader: true,
  143. componentScrollBar: null,
  144. componentScrollBoxElement: null
  145. };
  146. },
  147. watch: {
  148. '$route.path'() {
  149. // 触发伪滚动条更新
  150. this.componentScrollBox.scrollTop = 0;
  151. this.$nextTick(() => {
  152. this.componentScrollBar.update();
  153. });
  154. }
  155. },
  156. methods: {
  157. renderAnchorHref() {
  158. if (/changelog/g.test(location.href)) return;
  159. const anchors = document.querySelectorAll('h2 a,h3 a,h4 a,h5 a');
  160. const basePath = location.href.split('#').splice(0, 2).join('#');
  161. [].slice.call(anchors).forEach(a => {
  162. const href = a.getAttribute('href');
  163. a.href = basePath + href;
  164. });
  165. },
  166. goAnchor() {
  167. if (location.href.match(/#/g).length > 1) {
  168. const anchor = location.href.match(/#[^#]+$/g);
  169. if (!anchor) return;
  170. const elm = document.querySelector(anchor[0]);
  171. if (!elm) return;
  172. setTimeout(_ => {
  173. this.componentScrollBox.scrollTop = elm.offsetTop;
  174. }, 50);
  175. }
  176. },
  177. handleScroll() {
  178. const scrollTop = this.componentScrollBox.scrollTop;
  179. if (this.showHeader !== this.scrollTop > scrollTop) {
  180. this.showHeader = this.scrollTop > scrollTop;
  181. }
  182. if (scrollTop === 0) {
  183. this.showHeader = true;
  184. }
  185. if (!this.navFaded) {
  186. bus.$emit('fadeNav');
  187. }
  188. this.scrollTop = scrollTop;
  189. }
  190. },
  191. computed: {
  192. showBackToTop() {
  193. return !this.$route.path.match(/backtop/);
  194. }
  195. },
  196. created() {
  197. bus.$on('navFade', val => {
  198. this.navFaded = val;
  199. });
  200. },
  201. mounted() {
  202. this.componentScrollBar = this.$refs.componentScrollBar;
  203. this.componentScrollBox = this.componentScrollBar.$el.querySelector('.el-scrollbar__wrap');
  204. this.throttledScrollHandler = throttle(300, this.handleScroll);
  205. this.componentScrollBox.addEventListener('scroll', this.throttledScrollHandler);
  206. this.renderAnchorHref();
  207. this.goAnchor();
  208. document.body.classList.add('is-component');
  209. },
  210. destroyed() {
  211. document.body.classList.remove('is-component');
  212. },
  213. beforeDestroy() {
  214. this.componentScrollBox.removeEventListener('scroll', this.throttledScrollHandler);
  215. },
  216. beforeRouteUpdate(to, from, next) {
  217. next();
  218. setTimeout(() => {
  219. if (location.href.match(/#/g).length < 2) {
  220. document.documentElement.scrollTop = document.body.scrollTop = 0;
  221. this.renderAnchorHref();
  222. } else {
  223. this.goAnchor();
  224. }
  225. }, 100);
  226. }
  227. };
  228. </script>