component.tpl 7.7 KB

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