fs-readmore.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view
  3. class="fs-readmore"
  4. :style="{ height: isOpen ? 'auto' : `${height + 70}rpx`, paddingBottom: isOpen ? '70rpx' : 0, color }"
  5. >
  6. <view class="fs-readmore-content layout-box" :style="{ backgroundColor: bgColor }"><slot></slot></view>
  7. <view class="fs-readmore-footer" :style="{ backgroundColor: bgColor }" @click="handleToggle" v-if="visible">
  8. <view>{{ isOpen ? '收起' : label }}</view>
  9. <fs-icon class="fs-readmore-icon" type="icon-d-down" size="26rpx" :class="{ isOpen }"></fs-icon>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. /**
  15. * 展开更多组件
  16. * @description 展开更多组件
  17. * @property {Number} maxHeight 限定多高时展示更多按钮(单位rpx)
  18. * @property {String} label "更多"文本
  19. * @property {Boolean} open 是否展开
  20. * @property {String} bgColor 背景色
  21. * @property {String} color 文本颜色
  22. */
  23. export default {
  24. name: 'fs-readmore',
  25. props: {
  26. // 限定多高时展示更多按钮
  27. maxHeight: {
  28. type: Number,
  29. default: 100
  30. },
  31. label: {
  32. type: String,
  33. default: '展开更多'
  34. },
  35. open: Boolean,
  36. bgColor: {
  37. type: String,
  38. default: '#fff'
  39. },
  40. color: {
  41. type: String,
  42. default: '#666666'
  43. }
  44. },
  45. data() {
  46. return {
  47. isOpen: false,
  48. visible: false
  49. }
  50. },
  51. mounted() {
  52. this.updateHeight()
  53. },
  54. computed: {
  55. height() {
  56. return parseFloat(this.maxHeight)
  57. }
  58. },
  59. methods: {
  60. handleToggle() {
  61. this.isOpen = !this.isOpen
  62. },
  63. updateHeight() {
  64. uni
  65. .createSelectorQuery()
  66. .in(this)
  67. .select('.fs-readmore-content')
  68. .boundingClientRect(data => {
  69. this.visible = data.height > this.height
  70. })
  71. .exec()
  72. }
  73. }
  74. }
  75. </script>
  76. <style lang="scss" scoped>
  77. .fs-readmore {
  78. position: relative;
  79. overflow: hidden;
  80. &-content {
  81. overflow: hidden;
  82. }
  83. &-footer {
  84. display: flex;
  85. align-items: center;
  86. justify-content: center;
  87. position: absolute;
  88. z-index: 10;
  89. width: 100%;
  90. bottom: 0;
  91. font-size: 13px;
  92. height: 70rpx;
  93. line-height: 70rpx;
  94. // box-shadow: 0 -6rpx 2rpx rgba(65, 65, 70, 0.2);
  95. }
  96. &-icon {
  97. transition: all 0.2s;
  98. margin-left: 6rpx;
  99. }
  100. .isOpen {
  101. transform: rotate(180deg);
  102. }
  103. }
  104. </style>