component.tpl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. .page-component-up {
  87. background-color: #fff;
  88. position: fixed;
  89. right: 100px;
  90. bottom: 150px;
  91. size: 40px;
  92. border-radius: 20px;
  93. cursor: pointer;
  94. transition: .3s;
  95. box-shadow: 0 0 6px rgba(0,0,0, .12);
  96. z-index: 5;
  97. i {
  98. color: #409EFF;
  99. display: block;
  100. line-height: 40px;
  101. text-align: center;
  102. font-size: 18px;
  103. }
  104. &.hover {
  105. opacity: 1;
  106. }
  107. }
  108. .back-top-fade-enter,
  109. .back-top-fade-leave-active {
  110. transform: translateY(-30px);
  111. opacity: 0;
  112. }
  113. }
  114. @media (min-width: 1140px) {
  115. .page-component__content {
  116. transition:padding-right 0.3s ease;
  117. &.theme-config {
  118. padding-right: 26%;
  119. }
  120. }
  121. .page-container.page-component {
  122. transition:width 0.3s ease;
  123. &.theme-config {
  124. width: 98%;
  125. }
  126. .page-component__nav {
  127. padding-left: 2%;
  128. }
  129. }
  130. }
  131. @media (min-width: 1600px) {
  132. .page-component__content {
  133. &.theme-config {
  134. padding-right: 25%;
  135. }
  136. }
  137. .page-container.page-component {
  138. &.theme-config {
  139. width: 1600px;
  140. }
  141. }
  142. }
  143. @media (max-width: 768px) {
  144. .page-component {
  145. .page-component__nav {
  146. width: 100%;
  147. position: static;
  148. margin-top: 0;
  149. }
  150. .side-nav {
  151. padding-top: 0;
  152. padding-left: 50px;
  153. }
  154. .page-component__content {
  155. padding-left: 10px;
  156. padding-right: 10px;
  157. }
  158. .content {
  159. padding-top: 0;
  160. }
  161. .content > table {
  162. overflow: auto;
  163. display: block;
  164. }
  165. .page-component-up {
  166. display: none;
  167. }
  168. }
  169. }
  170. </style>
  171. <template>
  172. <el-scrollbar class="page-component__scroll" ref="componentScrollBar">
  173. <div class="page-container page-component" :class="{'theme-config': isThemeConfigVisible}">
  174. <el-scrollbar class="page-component__nav">
  175. <side-nav :data="navsData[lang]" :base="`/${ lang }/component`"></side-nav>
  176. </el-scrollbar>
  177. <div class="page-component__content" :class="{'theme-config': isThemeConfigVisible}">
  178. <router-view class="content"></router-view>
  179. <footer-nav></footer-nav>
  180. </div>
  181. <transition name="back-top-fade">
  182. <div
  183. class="page-component-up"
  184. :class="{ 'hover': hover }"
  185. v-show="showBackToTop"
  186. @mouseenter="hover = true"
  187. @mouseleave="hover = false"
  188. @click="toTop">
  189. <i class="el-icon-caret-top"></i>
  190. </div>
  191. </transition>
  192. </div>
  193. </el-scrollbar>
  194. </template>
  195. <script>
  196. import bus from '../../bus';
  197. import navsData from '../../nav.config.json';
  198. import throttle from 'throttle-debounce/throttle';
  199. export default {
  200. data() {
  201. return {
  202. lang: this.$route.meta.lang,
  203. navsData,
  204. hover: false,
  205. showBackToTop: false,
  206. scrollTop: 0,
  207. showHeader: true,
  208. componentScrollBar: null,
  209. componentScrollBoxElement: null,
  210. isThemeConfigVisible: false
  211. };
  212. },
  213. watch: {
  214. '$route.path'() {
  215. // 触发伪滚动条更新
  216. this.componentScrollBox.scrollTop = 0;
  217. this.$nextTick(() => {
  218. this.componentScrollBar.update();
  219. });
  220. }
  221. },
  222. methods: {
  223. renderAnchorHref() {
  224. if (/changelog/g.test(location.href)) return;
  225. const anchors = document.querySelectorAll('h2 a,h3 a,h4 a,h5 a');
  226. const basePath = location.href.split('#').splice(0, 2).join('#');
  227. [].slice.call(anchors).forEach(a => {
  228. const href = a.getAttribute('href');
  229. a.href = basePath + href;
  230. });
  231. },
  232. goAnchor() {
  233. if (location.href.match(/#/g).length > 1) {
  234. const anchor = location.href.match(/#[^#]+$/g);
  235. if (!anchor) return;
  236. const elm = document.querySelector(anchor[0]);
  237. if (!elm) return;
  238. setTimeout(_ => {
  239. this.componentScrollBox.scrollTop = elm.offsetTop;
  240. }, 50);
  241. }
  242. },
  243. toTop() {
  244. this.hover = false;
  245. this.showBackToTop = false;
  246. this.componentScrollBox.scrollTop = 0;
  247. },
  248. handleScroll() {
  249. const scrollTop = this.componentScrollBox.scrollTop;
  250. this.showBackToTop = scrollTop >= 0.5 * document.body.clientHeight;
  251. if (this.showHeader !== this.scrollTop > scrollTop) {
  252. this.showHeader = this.scrollTop > scrollTop;
  253. }
  254. if (scrollTop === 0) {
  255. this.showHeader = true;
  256. }
  257. if (!this.navFaded) {
  258. bus.$emit('fadeNav');
  259. }
  260. this.scrollTop = scrollTop;
  261. }
  262. },
  263. created() {
  264. bus.$on('navFade', val => {
  265. this.navFaded = val;
  266. });
  267. bus.$on('user-theme-config-visible', val => {
  268. this.isThemeConfigVisible = val;
  269. });
  270. window.addEventListener('hashchange', () => {
  271. if (location.href.match(/#/g).length < 2) {
  272. document.documentElement.scrollTop = document.body.scrollTop = 0;
  273. this.renderAnchorHref();
  274. } else {
  275. this.goAnchor();
  276. }
  277. });
  278. },
  279. mounted() {
  280. if (window.userThemeConfigVisible) {
  281. this.isThemeConfigVisible = window.userThemeConfigVisible;
  282. }
  283. this.componentScrollBar = this.$refs.componentScrollBar;
  284. this.componentScrollBox = this.componentScrollBar.$el.querySelector('.el-scrollbar__wrap');
  285. this.throttledScrollHandler = throttle(300, this.handleScroll);
  286. this.componentScrollBox.addEventListener('scroll', this.throttledScrollHandler);
  287. this.renderAnchorHref();
  288. this.goAnchor();
  289. document.body.classList.add('is-component');
  290. },
  291. destroyed() {
  292. document.body.classList.remove('is-component');
  293. },
  294. beforeDestroy() {
  295. this.componentScrollBox.removeEventListener('scroll', this.throttledScrollHandler);
  296. }
  297. };
  298. </script>