textShrink.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="font-shrink-box" >
  3. <view class="content-borderbox" :style="openStauts?'height:auto;':'max-height:'+(line*(size+16))+'rpx;overflow: hidden;'" >
  4. <view class="content-text" :style="'line-height:'+(size+16)+'rpx;font-size:'+size+'rpx'">
  5. {{text}}
  6. </view>
  7. <view v-if="isShowBtn&&!openStauts&&!hideEllips" class="ellipsis" :style="'line-height:'+(size+16)+'rpx;font-size:'+size+'rpx;height:'+(size+16)+'rpx;background-color:'+ellipsisBack">
  8. .....
  9. </view>
  10. </view>
  11. <view v-if="isShowBtn" class="open-close-btn" :style="'font-size:'+btnSize+'rpx'" @click="showAll">
  12. {{openStauts?'收起':'展开'}}
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. props:{
  19. line:{ //行数
  20. type:Number,
  21. default:2
  22. },
  23. text:{ // 文字
  24. type:String,
  25. default:''
  26. },
  27. size:{ //字体大小,单位rpx
  28. type:Number,
  29. default:28
  30. },
  31. btnSize:{
  32. type:Number,
  33. default:28
  34. },
  35. ellipsisBack:{ //省略号背景色
  36. type:String,
  37. default:'#fff'
  38. },
  39. hideEllips:{ //是否隐藏省略号
  40. type:Boolean,
  41. default:false
  42. }
  43. },
  44. data() {
  45. return {
  46. oldText:this.text,//旧的text
  47. borderHeight:0, //外层盒子的高度
  48. contentHeight:0, //放文字的盒子高度
  49. isShowBtn:false,//是否显示 展开/收起按钮
  50. openStauts:false,//展开状态,false-展开,true-收起
  51. };
  52. },
  53. updated() {
  54. // 使用oldText存放上一次的值,根据判断来减少一次方法调用
  55. if(this.oldText != this.text){
  56. this.oldText = this.text;
  57. this.initBtnShowStauts();
  58. }
  59. },
  60. mounted(){
  61. this.oldText = this.text;
  62. this.initBtnShowStauts();
  63. },
  64. methods:{
  65. initBtnShowStauts(){
  66. const query = uni.createSelectorQuery().in(this);
  67. let p1 = new Promise((resolve)=>{
  68. this.$nextTick(()=>{
  69. query.select('.content-borderbox').fields({ size: true, scrollOffset: true },
  70. data => { resolve(data.height) }).exec();
  71. })
  72. })
  73. let p2 = new Promise((resolve)=>{
  74. this.$nextTick(()=>{
  75. query.select('.content-text').fields({ size: true, scrollOffset: true },
  76. data => { resolve(data.height) }).exec();
  77. })
  78. })
  79. Promise.all([p1,p2]).then((res)=>{
  80. this.borderHeight = res[0];
  81. this.contentHeight = res[1];
  82. this.isShowBtn = this.borderHeight<this.contentHeight;
  83. });
  84. },
  85. showAll(){
  86. this.openStauts = !this.openStauts;
  87. }
  88. }
  89. }
  90. </script>
  91. <style lang="scss">
  92. .font-shrink-box{
  93. width: 100%;
  94. .content-borderbox{
  95. position: relative;
  96. .ellipsis{
  97. position: absolute;
  98. right: 0;
  99. bottom:0;
  100. }
  101. }
  102. .open-close-btn{
  103. width: 100%;
  104. color:#2250FF;
  105. text-align: center;
  106. padding:10rpx 0;
  107. }
  108. }
  109. </style>