You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

164 lines
4.0 KiB

  1. <template>
  2. <view class="">
  3. <view class="u-content" :class="[elId]" :style="{ height: isLongContent && !showMore ? showHeight + 'rpx' : 'auto' }">
  4. <slot></slot>
  5. </view>
  6. <view @tap="toggleReadMore" v-if="isLongContent" class="u-showmore-wrap"
  7. :class="{ 'u-show-more': showMore }"
  8. :style="[innerShadowStyle]"
  9. >
  10. <text class="u-readmore-btn" :style="{
  11. fontSize: fontSize + 'rpx',
  12. color: color
  13. }">
  14. {{ showMore ? openText : closeText }}
  15. </text>
  16. <u-icon :color="color" :size="fontSize" class="u-icon" :name="showMore ? 'arrow-up' : 'arrow-down'"></u-icon>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. /**
  22. * readMore 阅读更多
  23. * @description 该组件一般用于内容较长,预先收起一部分,点击展开全部内容的场景。
  24. * @tutorial https://www.uviewui.com/components/readMore.html
  25. * @property {String Number} show-height 内容超出此高度才会显示展开全文按钮,单位rpx(默认400)
  26. * @property {Boolean} toggle 展开后是否显示收起按钮(默认false)
  27. * @property {String} close-text 关闭时的提示文字(默认“展开阅读全文”)
  28. * @property {String Number} font-size 提示文字的大小,单位rpx(默认28)
  29. * @property {String} open-text 展开时的提示文字(默认“收起”)
  30. * @property {String} color 提示文字的颜色(默认#2979ff)
  31. * @example <u-read-more><rich-text :nodes="content"></rich-text></u-read-more>
  32. */
  33. export default {
  34. name: "u-read-more",
  35. props: {
  36. // 默认的显示占位高度,单位为rpx
  37. showHeight: {
  38. type: [Number, String],
  39. default: 400
  40. },
  41. // 展开后是否显示"收起"按钮
  42. toggle: {
  43. type: Boolean,
  44. default: false
  45. },
  46. // 关闭时的提示文字
  47. closeText: {
  48. type: String,
  49. default: '展开阅读全文'
  50. },
  51. // 展开时的提示文字
  52. openText: {
  53. type: String,
  54. default: '收起'
  55. },
  56. // 提示的文字颜色
  57. color: {
  58. type: String,
  59. default: '#2979ff'
  60. },
  61. // 提示文字的大小
  62. fontSize: {
  63. type: [String, Number],
  64. default: 28
  65. },
  66. // 是否显示阴影
  67. shadowStyle: {
  68. type: Object,
  69. default() {
  70. return {
  71. backgroundImage: "linear-gradient(-180deg, rgba(255, 255, 255, 0) 0%, #fff 80%)",
  72. paddingTop: "300rpx",
  73. marginTop: "-300rpx"
  74. }
  75. }
  76. }
  77. },
  78. watch: {
  79. paramsChange(val) {
  80. this.init();
  81. }
  82. },
  83. computed: {
  84. paramsChange() {
  85. return `${this.toggle}-${this.showHeight}`;
  86. },
  87. // 展开后无需阴影,收起时才需要阴影样式
  88. innerShadowStyle() {
  89. if(this.showMore) return {};
  90. else return this.shadowStyle
  91. }
  92. },
  93. data() {
  94. return {
  95. isLongContent: false, // 是否需要隐藏一部分内容
  96. showMore: false, // 当前隐藏与显示的状态,true-显示,false-收起
  97. elId: this.$u.guid(), // 生成唯一class
  98. };
  99. },
  100. mounted() {
  101. this.$nextTick(function(){
  102. this.init();
  103. })
  104. },
  105. methods: {
  106. init() {
  107. this.$uGetRect('.' + this.elId).then(res => {
  108. // 判断高度,如果真实内容高度大于占位高度,则显示收起与展开的控制按钮
  109. if (res.height > uni.upx2px(this.showHeight)) {
  110. this.isLongContent = true;
  111. this.showMore = false;
  112. }
  113. })
  114. },
  115. // 展开或者收起
  116. toggleReadMore() {
  117. this.showMore = !this.showMore;
  118. // 如果toggle为false,隐藏"收起"部分的内容
  119. if (this.toggle == false) this.isLongContent = false;
  120. }
  121. }
  122. };
  123. </script>
  124. <style lang="scss" scoped>
  125. @import "../../libs/css/style.components.scss";
  126. .u-content {
  127. font-size: 30rpx;
  128. color: $u-content-color;
  129. line-height: 1.8;
  130. text-align: left;
  131. text-indent: 2em;
  132. overflow: hidden;
  133. }
  134. .u-showmore-wrap {
  135. position: relative;
  136. width: 100%;
  137. padding-bottom: 26rpx;
  138. display: flex;
  139. align-items: center;
  140. justify-content: center;
  141. }
  142. .u-show-more {
  143. padding-top: 0;
  144. background: none;
  145. margin-top: 20rpx;
  146. }
  147. .u-readmore-btn {
  148. display: flex;
  149. align-items: center;
  150. justify-content: center;
  151. line-height: 1;
  152. }
  153. .u-icon {
  154. margin-left: 14rpx;
  155. }
  156. </style>