fs-readmore.vue 1.8 KB

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