fs-readmore.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  26. export default {
  27. props: {
  28. // 限定多高时展示更多按钮
  29. maxHeight: {
  30. type: Number,
  31. default: 100
  32. },
  33. label: {
  34. type: String,
  35. default: '展开更多'
  36. },
  37. open: Boolean,
  38. bgColor: {
  39. type: String,
  40. default: '#fff'
  41. },
  42. color: {
  43. type: String,
  44. default: '#666666'
  45. }
  46. },
  47. data() {
  48. return {
  49. isOpen: false,
  50. visible: false
  51. }
  52. },
  53. mounted() {
  54. this.updateHeight()
  55. },
  56. computed: {
  57. height() {
  58. return parseFloat(this.maxHeight)
  59. }
  60. },
  61. methods: {
  62. handleToggle() {
  63. this.isOpen = !this.isOpen
  64. },
  65. updateHeight() {
  66. uni
  67. .createSelectorQuery()
  68. .in(this)
  69. .select('.fs-readmore-content')
  70. .boundingClientRect(data => {
  71. this.visible = data.height > this.height
  72. })
  73. .exec()
  74. }
  75. }
  76. }
  77. </script>
  78. <style lang="scss" scoped>
  79. .fs-readmore {
  80. position: relative;
  81. overflow: hidden;
  82. &-content {
  83. overflow: hidden;
  84. }
  85. &-footer {
  86. display: flex;
  87. align-items: center;
  88. justify-content: center;
  89. position: absolute;
  90. z-index: 10;
  91. width: 100%;
  92. bottom: 0;
  93. font-size: 13px;
  94. height: 70rpx;
  95. line-height: 70rpx;
  96. // box-shadow: 0 -6rpx 2rpx rgba(65, 65, 70, 0.2);
  97. }
  98. &-icon {
  99. transition: all 0.2s;
  100. margin-left: 6rpx;
  101. }
  102. .isOpen {
  103. transform: rotate(180deg);
  104. }
  105. }
  106. </style>