test.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view class="intro">
  3. <view class="introText">
  4. <span v-if="introduce" ref="text" @click="clickTotal">
  5. {{introduce}}
  6. </span>
  7. <span v-if='exchangeButton' @click="clickTotal">
  8. {{introduceNew}}
  9. </span>
  10. <view class="unfold" @click="clickTotal" v-if="showExchangeButton">
  11. <span>{{showTotal ? '展开' : '收起'}}</span>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. title: "演示展开收起",
  21. introduce: "",
  22. introduceNew: "",
  23. introduceOld: "",
  24. // 是否展示所有文本内容
  25. showTotal: true,
  26. // 显示展开还是收起
  27. exchangeButton: false,
  28. // 是否显示展开收起按钮
  29. showExchangeButton: false,
  30. rem: ""
  31. }
  32. },
  33. mounted() {
  34. this.init();
  35. },
  36. methods: {
  37. clickTotal() {
  38. this.showTotal = !this.showTotal;
  39. if (this.showTotal && this.showExchangeButton == true) {
  40. this.$refs.text.innerHTML = this.introduceNew
  41. } else {
  42. this.$refs.text.innerHTML = this.introduceOld
  43. }
  44. },
  45. getRem() {
  46. const defaultRem = 16;
  47. let winWidth = window.innerWidth;
  48. let rem = (winWidth / 375) * defaultRem;
  49. return rem;
  50. },
  51. init() {
  52. this.introduce =
  53. "唐朝,可谓歌舞盛华,乐文辉煌。与此同时,女伎们的身价也推向了巅峰。青楼之市火爆,歌舞伎成了热门之业。沦落为青楼女子虽不光彩,但依然有很多人选择这一职业,在这些女子中,琴棋书画兼备的也大有人在长安城内,有一青楼,名为“玉满楼”,别名“玉月满花”,此中之女子皆是琴棋书画兼备,相传呐,那都是卖艺不卖身的存在。我们的故事就要从这玉满楼开始说起。在长安城内,有一青楼,名为“玉满楼”,别名“玉月满花”,此中之女子皆是琴棋书画兼备,相传呐,那都是卖艺不卖身的存在。我们的故事就要从这玉满楼开始说起。";
  54. this.introduceOld =
  55. "唐朝,可谓歌舞盛华,乐文辉煌。与此同时,女伎们的身价也推向了巅峰。青楼之市火爆,歌舞伎成了热门之业。沦落为青楼女子虽不光彩,但依然有很多人选择这一职业,在这些女子中,琴棋书画兼备的也大有人在长安城内,有一青楼,名为“玉满楼”,别名“玉月满花”,此中之女子皆是琴棋书画兼备,相传呐,那都是卖艺不卖身的存在。我们的故事就要从这玉满楼开始说起。在长安城内,有一青楼,名为“玉满楼”,别名“玉月满花”,此中之女子皆是琴棋书画兼备,相传呐,那都是卖艺不卖身的存在。我们的故事就要从这玉满楼开始说起。";
  56. },
  57. },
  58. watch: {
  59. introduce: function() {
  60. this.$nextTick(
  61. function() {
  62. // 判断介绍是否超过四行
  63. let rem = parseFloat(this.getRem());
  64. if (!this.$refs.text) {
  65. return;
  66. }
  67. let rows = this.$refs.text.getClientRects().length // 文本行数
  68. let txt = this.introduceOld // 文本
  69. console.log(txt)
  70. // 文本为3行时的字数长度
  71. let tl = 0 // eslint-disable-line no-unused-vars
  72. // 数据量大时速度太慢,需优化(二分法?)
  73. while (rows > 4) { // 超出,遍历文字并进行截取,直到文本小于三行 显示4行
  74. let step = 1 // 末尾扣除的字数
  75. if (/<br\/>$/.test(txt)) { // 回退的时候,如果碰到换行要整体替换
  76. step = 5
  77. }
  78. txt = txt.slice(0, -step) // 截取
  79. this.showExchangeButton = true
  80. this.$refs.text.innerHTML = txt
  81. rows = this.$refs.text.getClientRects().length
  82. tl += step
  83. }
  84. while (rows < 4) { //、显示4行
  85. txt = txt
  86. this.$refs.text.innerHTML = txt
  87. return
  88. }
  89. let num = 0;
  90. if (txt.charCodeAt(txt.length - 1) < 255) {
  91. num = 2
  92. } else {
  93. num = 1
  94. }
  95. if (txt.charCodeAt(txt.length - 2) < 255) {
  96. num = num + 2
  97. } else {
  98. num = num + 1
  99. }
  100. if (txt.charCodeAt(txt.length - 4) < 255) {
  101. num = num + 2
  102. } else {
  103. num = num + 1
  104. }
  105. this.$refs.text.innerHTML = txt.slice(0, -1 * num) + '...'
  106. this.introduceNew = txt.slice(0, -1 * num) + '...'
  107. }.bind(this)
  108. );
  109. }
  110. },
  111. }
  112. </script>
  113. <style>
  114. .intro {
  115. width: 95%;
  116. border: 2px solid grey;
  117. padding: 10px;
  118. margin: 0 auto;
  119. box-sizing: border-box;
  120. overflow: hidden;
  121. }
  122. .intro .introText {
  123. text-align: justify;
  124. position: relative;
  125. display: block;
  126. }
  127. .intro .introText span {
  128. font-size: 14px;
  129. color: #627081;
  130. }
  131. .intro .unfold {
  132. position: absolute;
  133. right: 0;
  134. bottom: 0;
  135. opacity: 1;
  136. }
  137. .intro .unfold span {
  138. color: #2878ff;
  139. cursor: pointer;
  140. font-size: 30rpx;
  141. }
  142. </style>