text-over-flow.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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="text-overflow-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: #E95700 ;">{{ expandText }}</text>
  11. </view>
  12. </view>
  13. <view v-else>
  14. <view>
  15. <text class="text-overflow-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('.text-overflow-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. // console.log(this.line,this.textHeight,this.lineHeight, Math.floor( this.textHeight / this.lineHeight ),11111111)
  106. return Math.floor(this.textHeight > 0 && this.lineHeight > 0 ? this.textHeight / this.lineHeight : 0)
  107. }
  108. }
  109. }
  110. </script>
  111. <style scoped>
  112. .dt-content {
  113. overflow: hidden;
  114. text-overflow: clip;
  115. display: -webkit-box;
  116. -webkit-box-orient: vertical;
  117. }
  118. .button-show {
  119. width: 70rpx;
  120. position: absolute;
  121. right: 8px;
  122. bottom: 0;
  123. z-index: 0;
  124. text-align: right;
  125. background-image: linear-gradient(-180deg, rgba(233, 236, 239, 0) 50%, #FFF 80%);
  126. padding-top: 2rem;
  127. }
  128. .fold-hint {
  129. color: #E95700;
  130. text-align: right;
  131. position:relative;
  132. right:12px;
  133. }
  134. .text-overflow-content{
  135. letter-spacing:1px;
  136. }
  137. </style>