kevy-ellipsis.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view class="t-wrap">
  3. <view class="t-txt-hide" :id="hid" v-if="!isCompute" :style="[computeStyle(0)]">
  4. {{testContent?testContent:content}}{{showSymbol?'...':''}}
  5. <text v-if="(expandText || collapseText)&&rows>0"
  6. class="t-button">
  7. {{expandText}}
  8. </text>
  9. </view>
  10. <view class="t-ellipsis" :id="id" :style="[!isCompute?computeStyle(1):computeStyle(2)]" @click="contentClick">
  11. {{((!isCompute || expand)&&rows>0)?content:(actualContent+(showSymbol?'...':''))}}<text
  12. v-if="(expandText || collapseText) && showSymbol" class="t-button" @click.stop="changeCollapse"
  13. :style="{'color':actionFontColor,'float':'right'}">{{!expand?expandText:collapseText}}</text>
  14. </view>
  15. <!-- #ifdef H5 -->
  16. <view v-if="!isCompute && rows>0" class="t-skeleton">
  17. <view class="skeletons" v-for="(item,index) in rows">
  18. <view class="empty"></view>
  19. </view>
  20. </view>
  21. <!-- #endif -->
  22. </view>
  23. </template>
  24. <script>
  25. import {
  26. init,
  27. computeStyle,
  28. compute
  29. } from './kevy-ellipsis.min.js'
  30. export default {
  31. name: "kevy-ellipsis",
  32. props: {
  33. /**
  34. * 文本内容,默认''
  35. */
  36. content: {
  37. type: String,
  38. default: ''
  39. },
  40. /**
  41. * 字体大小,单位rpx,默认28
  42. */
  43. fontSize: {
  44. type: Number,
  45. default: '28'
  46. },
  47. /**
  48. * 字体颜色,默认#666666
  49. */
  50. fontColor: {
  51. color: String,
  52. default: '#666666'
  53. },
  54. fontWeight: {
  55. color: String,
  56. default: '400'
  57. },
  58. /**
  59. * 收起操作的文案,默认''
  60. */
  61. collapseText: {
  62. type: String,
  63. default: ''
  64. },
  65. /**
  66. * 展开操作的文案,默认''
  67. */
  68. expandText: {
  69. type: String,
  70. default: ''
  71. },
  72. /**
  73. * 收起、展开操作文字颜色,默认'#007aff'
  74. */
  75. actionFontColor: {
  76. color: String,
  77. default: '#007aff'
  78. },
  79. /**
  80. * 展示行数,默认1
  81. */
  82. rows:{
  83. type: [String,Number],
  84. default: 1
  85. },
  86. },
  87. data() {
  88. return {
  89. //是否展开
  90. expand: false,
  91. //是否已计算
  92. isCompute: false,
  93. //内容高度
  94. h: undefined,
  95. //内容宽度
  96. w: undefined,
  97. //实际显示内容
  98. actualContent: '',
  99. //高度探测内容
  100. testContent: undefined,
  101. //是否显示省略号
  102. showSymbol: false,
  103. //hid和id,唯一标识符
  104. hid: 'hid' + Math.random().toString(36).substr(2),
  105. id: 'id' + Math.random().toString(36).substr(2),
  106. };
  107. },
  108. created(){},
  109. mounted() {
  110. if (this.content?.length > 0) {
  111. // #ifdef H5
  112. this.$nextTick(() => {
  113. init(this, () => {
  114. compute(this);
  115. })
  116. })
  117. // #endif
  118. // #ifdef MP-ALIPAY
  119. init(this, () => {
  120. compute(this, true);
  121. }, true)
  122. // #endif
  123. // #ifndef MP-ALIPAY || H5
  124. init(this, () => {
  125. compute(this);
  126. })
  127. // #endif
  128. }
  129. },
  130. computed: {
  131. computeStyle
  132. },
  133. methods: {
  134. //收起展开状态切换
  135. changeCollapse() {
  136. this.$emit('moreClick')
  137. if(!this.collapseText) return;
  138. this.expand = !this.expand;
  139. },
  140. //文本点击事件
  141. contentClick() {
  142. this.$emit('contentClick');
  143. },
  144. },
  145. watch:{
  146. isCompute:{
  147. handler(newval){
  148. console.log(newval,'newval')
  149. },
  150. immediate: true,
  151. deep: true
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. .t-wrap {
  158. // width: var(--width);
  159. width:100%;
  160. box-sizing: border-box;
  161. position: relative;
  162. }
  163. .t-txt-hide {
  164. word-break: break-word;
  165. position: absolute;
  166. top: 999999px;
  167. left: 999999px;
  168. z-index: -1000;
  169. top: 0rpx;
  170. width: 100%;
  171. margin: 0rpx;
  172. text-align: justify;
  173. }
  174. .t-ellipsis {
  175. text-align: end;
  176. box-sizing: border-box;
  177. width: 100%;
  178. word-break: break-word;
  179. position: relative;
  180. left: 99999px;
  181. }
  182. .t-skeleton{
  183. width: 100%;
  184. height: 100%;
  185. box-sizing: border-box;
  186. position: absolute;
  187. top: 0rpx;
  188. left: 0rpx;
  189. }
  190. .skeletons:first-child{
  191. margin-top: 0rpx !important;
  192. }
  193. .skeletons {
  194. position: relative;
  195. display: block;
  196. overflow: hidden;
  197. width: 100%;
  198. height: 32rpx;
  199. margin-top: 12rpx;
  200. background-color: rgba(0, 0, 0, 0.06);
  201. box-sizing: border-box;
  202. }
  203. // .skeletons .empty {
  204. // display: block;
  205. // position: absolute;
  206. // width: 100%;
  207. // height: 100%;
  208. // -webkit-transform: translateX(-100%);
  209. // transform: translateX(-100%);
  210. // background: linear-gradient(90deg, transparent, rgba(216, 216, 216, 0.753), transparent);
  211. // -webkit-animation: loading .8s infinite;
  212. // animation: loading .8s infinite;
  213. // }
  214. // @keyframes loading {
  215. // 100% {
  216. // -webkit-transform: translateX(100%);
  217. // transform: translateX(100%);
  218. // }
  219. // }
  220. </style>