hidden.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <div
  3. class="swap"
  4. :class="{
  5. hasPadding: heightSlot > 400,
  6. }"
  7. >
  8. <div
  9. class="eidt-box"
  10. :class="{
  11. show: isShow,
  12. hide: isHide,
  13. }"
  14. >
  15. <slot></slot>
  16. </div>
  17. <div :title="titleMsg" v-if="heightSlot > 400" class="bar" @click="showall">
  18. <svg width="20" viewBox="0,0 20,10">
  19. <path
  20. v-if="isShow"
  21. d="M 0,5
  22. L10,10
  23. L 20,5"
  24. fill="none"
  25. stroke="#000"
  26. ></path>
  27. <path
  28. v-if="!isShow"
  29. d="M 0,5
  30. L10,0
  31. L 20,5"
  32. fill="none"
  33. stroke="#000"
  34. ></path>
  35. </svg>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. siteHeight: {
  44. height: '',
  45. },
  46. isShow: true,
  47. isHide: false,
  48. // 盒子高度
  49. heightSlot: 0,
  50. titleMsg: '点击展开',
  51. };
  52. },
  53. methods: {
  54. showall() {
  55. this.isShow = !this.isShow;
  56. this.isHide = !this.isHide;
  57. if (this.isHide) {
  58. this.siteHeight = { height: this.heightSlot + 'px' };
  59. this.titleMsg = '点击收起';
  60. } else {
  61. this.siteHeight = {
  62. height: '400px',
  63. };
  64. this.titleMsg = '点击展开';
  65. }
  66. },
  67. },
  68. mounted() {
  69. this.heightSlot = this.$slots.default[0].elm.offsetHeight;
  70. if (this.heightSlot < 400) {
  71. this.siteHeight = { height: '' };
  72. } else {
  73. this.siteHeight = { height: '300px' };
  74. }
  75. },
  76. };
  77. </script>
  78. <style lang="scss" scoped>
  79. .eidt-box {
  80. transition: all 0.5s;
  81. }
  82. .swap {
  83. position: relative;
  84. background: #f2f4f5;
  85. margin: 16px 0;
  86. }
  87. .hasPadding {
  88. padding-bottom: 30px;
  89. }
  90. .show {
  91. overflow: hidden;
  92. position: relative;
  93. max-height: 400px;
  94. }
  95. .hide {
  96. position: relative;
  97. max-height: 4000px;
  98. }
  99. .bar {
  100. height: 30px;
  101. width: 100%;
  102. bottom: 0;
  103. background: linear-gradient(rgba(255, 255, 255, 0), #fafafa);
  104. position: absolute;
  105. z-index: 1;
  106. text-align: center;
  107. cursor: pointer;
  108. svg {
  109. vertical-align: bottom;
  110. }
  111. }
  112. </style>