产品效果

少于5行文字,则全部显示,多于5行文字,则显示 “查看全文”; 点击展开不用收起

1632897803(1).png

方案思路

利用 父级盒子的高度和 overflow: hidden;position: absolute;

代码具体实现

HTML

  1. <!-- isShowIntro: 是否展开查看全文 -->
  2. <view
  3. class="item-content text-f15"
  4. :class="isShowIntro?'content-text line5':' content-text-show'">
  5. {{content || ''}}
  6. <span
  7. class="primary"
  8. v-if="isShowIntro"
  9. @click.stop="()=>{ this.isShowIntro=false }">
  10. 查看全文
  11. </span>
  12. </view>

style

  1. .item-content {
  2. text {
  3. line-height: 46rpx;
  4. }
  5. .content {
  6. margin-bottom: 8rpx;
  7. }
  8. .primary {
  9. font-size: 28rpx;
  10. color: #1EA0F1;
  11. }
  12. // 查看全文 - 收起
  13. &.content-text {
  14. position: relative;
  15. max-height: 228rpx;
  16. line-height: 46rpx;
  17. overflow: hidden;
  18. text-align: left;
  19. .primary {
  20. position: absolute;
  21. top: 186rpx;
  22. left: 0;
  23. width: 100%;
  24. height: 60rpx;
  25. background-color: #fff;
  26. }
  27. }
  28. // 查看全文 - 展开
  29. &.content-text-show {
  30. position: relative;
  31. line-height: 46rpx;
  32. .primary {
  33. position: absolute;
  34. bottom: 0;
  35. left: 0;
  36. width: 100%;
  37. background-color: #fff;
  38. display: none;
  39. opacity: 0;
  40. }
  41. }
  42. }