component.tpl 5.8 KB

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