demo-block.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div
  3. class="demo-block"
  4. :class="[blockClass, { 'hover': hovering }]"
  5. @mouseenter="hovering = true"
  6. @mouseleave="hovering = false">
  7. <slot></slot>
  8. <div class="demo-block-control" @click="isExpanded = !isExpanded">
  9. <i :class="iconClass"></i>
  10. <span v-show="hovering">{{ controlText }}</span>
  11. </div>
  12. </div>
  13. </template>
  14. <style>
  15. .demo-block {
  16. border: solid 1px #eaeefb;
  17. border-radius: 4px;
  18. transition: .2s;
  19. &.hover {
  20. box-shadow: 0 6px 18px 0 rgba(232, 237, 250, .5);
  21. }
  22. .source {
  23. padding: 24px;
  24. }
  25. .meta {
  26. background-color: #fbfcfd;
  27. border-top: solid 1px #eaeefb;
  28. clear: both;
  29. overflow: hidden;
  30. height: 0;
  31. transition: height .2s;
  32. }
  33. .description {
  34. padding: 18px 24px;
  35. width: 40%;
  36. box-sizing: border-box;
  37. border-left: solid 1px #eaeefb;
  38. float: right;
  39. font-size: 14px;
  40. line-height: 1.5;
  41. color: #5e6d82;
  42. word-break: break-word;
  43. p {
  44. margin: 0;
  45. }
  46. code {
  47. background-color: #f4faff;
  48. border: solid 1px #eaeefb;
  49. margin: 0 4px;
  50. padding: 0 4px;
  51. font-size: 12px;
  52. }
  53. }
  54. .highlight {
  55. width: 60%;
  56. border-right: solid 1px #eaeefb;
  57. pre {
  58. margin: 0;
  59. }
  60. code.hljs {
  61. padding: 18px 24px;
  62. margin: 0;
  63. line-height: 1.4;
  64. background-color: #fbfcfd;
  65. border: none;
  66. max-height: none;
  67. border-radius: 0;
  68. &::before {
  69. content: none;
  70. }
  71. }
  72. }
  73. .demo-block-control {
  74. border-top: solid 1px #eaeefb;
  75. height: 36px;
  76. box-sizing: border-box;
  77. background-color: #fff;
  78. border-bottom-left-radius: 4px;
  79. border-bottom-right-radius: 4px;
  80. text-align: center;
  81. margin-top: -1px;
  82. color: #d3dce6;
  83. cursor: pointer;
  84. transition: .2s;
  85. i {
  86. font-size: 12px;
  87. line-height: 36px;
  88. }
  89. span {
  90. font-size: 14px;
  91. line-height: 36px;
  92. }
  93. &:hover {
  94. color: #20a0ff;
  95. background-color: rgba(32, 159, 255, .05);
  96. }
  97. }
  98. }
  99. </style>
  100. <script type="text/babel">
  101. export default {
  102. data() {
  103. return {
  104. hovering: false,
  105. isExpanded: false
  106. };
  107. },
  108. computed: {
  109. blockClass() {
  110. return `demo-${ this.$router.currentRoute.path.split('/').pop() }`;
  111. },
  112. iconClass() {
  113. return this.isExpanded ? 'el-icon-caret-top' : 'el-icon-caret-bottom';
  114. },
  115. controlText() {
  116. return this.isExpanded ? '隐藏代码' : '显示代码';
  117. },
  118. codeArea() {
  119. return this.$el.getElementsByClassName('meta')[0];
  120. },
  121. codeAreaHeight() {
  122. if (this.$el.getElementsByClassName('description').length > 0) {
  123. return Math.max(this.$el.getElementsByClassName('description')[0].clientHeight, this.$el.getElementsByClassName('highlight')[0].clientHeight);
  124. }
  125. return this.$el.getElementsByClassName('highlight')[0].clientHeight;
  126. }
  127. },
  128. watch: {
  129. isExpanded(val) {
  130. this.codeArea.style.height = val ? `${ this.codeAreaHeight + 1 }px` : '0';
  131. }
  132. },
  133. mounted() {
  134. this.$nextTick(() => {
  135. let highlight = this.$el.getElementsByClassName('highlight')[0];
  136. if (this.$el.getElementsByClassName('description').length === 0) {
  137. highlight.style.width = '100%';
  138. highlight.borderRight = 'none';
  139. }
  140. });
  141. }
  142. };
  143. </script>