text-over-flow.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <view>
  3. <view style="position: relative" v-if="isHide">
  4. <view class="dt-content" :style="'-webkit-line-clamp:'+line">
  5. <text class="content">
  6. <slot>{{ dt ? dt : '' }}</slot>
  7. </text>
  8. </view>
  9. <view class="button-show" @tap="isHide = false" v-if="enableButton&&lines>line">
  10. <text style="color: #C82229">{{ expandText }}</text>
  11. </view>
  12. </view>
  13. <view v-else>
  14. <view>
  15. <text class="content">
  16. <slot>{{ dt ? dt : '' }}</slot>
  17. </text>
  18. </view>
  19. <view class="fold-hint" v-if="foldHint">
  20. <view @tap="isHide = true">
  21. {{ foldHint }}
  22. </view>
  23. </view>
  24. </view>
  25. <view>
  26. <text class="placeholder">
  27. {{ placeholder }}
  28. </text>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. // 是否隐藏多余行。初始状态不隐藏
  37. isHide: true,
  38. // 全量所占文本高度
  39. textHeight: 0,
  40. // 单行文本所占高度
  41. lineHeight: 1,
  42. // 占位文本
  43. placeholder: '占位'
  44. };
  45. },
  46. props: {
  47. // 展示多少行
  48. line: {
  49. type: [Number, String],
  50. default: 1
  51. },
  52. // 文本
  53. dt: {
  54. type: [String],
  55. default: ''
  56. },
  57. enableButton: {
  58. type: Boolean,
  59. default: true
  60. },
  61. // 自定义展开提示
  62. expandText: {
  63. type: String,
  64. default: "展开"
  65. },
  66. // 自定义收起提示
  67. foldHint: {
  68. type: String,
  69. default: "收起"
  70. }
  71. },
  72. watch:{
  73. dt(){
  74. let that = this
  75. setTimeout(() => {
  76. let query = uni.createSelectorQuery().in(that);
  77. // 获取所有文本在html中的高度
  78. query.select('.content').boundingClientRect(data => {
  79. that.textHeight = data.height
  80. }).exec();
  81. }, 100)
  82. }
  83. },
  84. mounted() {
  85. if (this.enableButton) {
  86. let query = uni.createSelectorQuery().in(this);
  87. // 获取所有文本在html中的高度
  88. query.select('.content').boundingClientRect(data => {
  89. this.textHeight = data.height
  90. }).exec();
  91. // 通过占位元素获取单行文本的高度
  92. query.select('.placeholder').boundingClientRect(data => {
  93. this.lineHeight = data.height
  94. }).exec();
  95. }
  96. // 获取单行文本高度后,置空占位元素,使其释放占位
  97. this.placeholder = ''
  98. },
  99. computed: {
  100. // 全文本所占总行数
  101. lines() {
  102. if (!this.enableButton) {
  103. return this.line
  104. }
  105. return Math.floor(this.textHeight > 0 && this.lineHeight > 0 ? this.textHeight / this.lineHeight : 0)
  106. }
  107. }
  108. }
  109. </script>
  110. <style scoped>
  111. .dt-content {
  112. overflow: hidden;
  113. text-overflow: clip;
  114. display: -webkit-box;
  115. -webkit-box-orient: vertical;
  116. }
  117. .button-show {
  118. width: 70rpx;
  119. position: absolute;
  120. right: 0;
  121. bottom: 0;
  122. z-index: 0;
  123. text-align: right;
  124. background-image: linear-gradient(-180deg, rgba(233, 236, 239, 0) 50%, #FFF 80%);
  125. padding-top: 2rem;
  126. }
  127. .fold-hint {
  128. color: #C82229;
  129. text-align: right
  130. }
  131. </style>