当前位置:   article > 正文

vue 文本超过2行展示更多:列表做法、单个文本做法_vue超过高度显示更多

vue超过高度显示更多
  1. <template>
  2. <div>
  3. <div class="group">
  4. <div class="text more" ref="more">占位</div>
  5. <div class="list" v-for="(item, index) in historyList" :key="index">
  6. <div
  7. class="text"
  8. ref="textContainer"
  9. :class="{ retract: item.status }"
  10. :style="{ 'max-height': item.status ? textHeight : '' }"
  11. >
  12. {{item.content }}
  13. </div>
  14. <span v-if="item.status !== null" class="link" @click="more(index)">{{
  15. item.status ? "展开" : "收起"
  16. }}</span>
  17. </div>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. import { Col, Row } from "vant";
  23. export default {
  24. data() {
  25. return {
  26. textHeight: "",
  27. content:"-1听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐;-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐",
  28. historyList: [
  29. {
  30. version: "7.1.4",
  31. title: "本次更新",
  32. content:
  33. "-2听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐;-听模块新增“文章难度分析”功能~,为你分析文章中词汇、语速等难度,推荐",
  34. time: "2周前",
  35. },
  36. ],
  37. };
  38. },
  39. components: {
  40. [Row.name]: Row,
  41. [Col.name]: Col,
  42. },
  43. mounted() {
  44. this.historyList.forEach((ele, index) => {
  45. this.$set( this.historyList, index, Object.assign({ }, ele, { status: null }) );
  46. });
  47. // DOM 加载完执行
  48. this.$nextTick(() => {
  49. this.calculateText();
  50. //console.log(this.historyList)
  51. });
  52. window.onresize = () => {
  53. this.historyList.forEach((ele, index) => {
  54. this.$set(
  55. this.historyList,
  56. index,
  57. Object.assign({ }, ele, { status: null })
  58. );
  59. });
  60. setTimeout(() => {
  61. this.calculateText();
  62. }, 0);
  63. };
  64. },
  65. methods: {
  66. // 计算文字 显示展开 收起
  67. calculateText() {
  68. // 获取一行文字的height 计算当前文字比较列表文字
  69. let oneHeight = this.$refs.more.scrollHeight;
  70. let twoHeight = oneHeight * 1 || 40;
  71. this.textHeight = `${ twoHeight}px`;
  72. let txtDom = this.$refs.textContainer;
  73. for (let i = 0; i < txtDom.length; i++) {
  74. let curHeight = txtDom[i].offsetHeight;
  75. if (curHeight > twoHeight) {
  76. this.$set(
  77. this.historyList,
  78. i,
  79. Object.assign({ }, this.historyList[i], { status: true })
  80. );
  81. } else {
  82. this.$set(
  83. this.historyList,
  84. i,
  85. Object.assign({ }, this.historyList[i], { status: null })
  86. );
  87. }
  88. }
  89. },
  90. more(index) {
  91. this.historyList[index].status = !this.historyList[index].status;
  92. },
  93. },
  94. };
  95. </script>
  96. <style lang="less" scoped>
  97. .group {
  98. .text {
  99. position: relative;
  100. color: #000;
  101. font-size: 14px;
  102. }
  103. .more {
  104. visibility: hidden;
  105. }
  106. .link {
  107. font-size: 12px;
  108. color: #2d95fe;
  109. }
  110. .retract {
  111. position: relative;
  112. overflow: hidden;
  113. }
  114. .retract:after {
  115. content: "...";
  116. position: absolute;
  117. bottom: 0;
  118. right: 2px;
  119. width: 25px;
  120. padding-left: 25px;
  121. background: linear-gradient(to right, transparent, #fff 45%);
  122. }
  123. }
  124. </style>

vue 文本超过2行展示更多:单个文本做法

  1. <template>
  2. <div>
  3. <div class="group">
  4. <div class="text2 more2" ref="more2">占位不可删除</div>
  5. <div class="list2">
  6. <div
  7. class="text2"
  8. ref="textContainer2"
  9. :class="{ retract2: testObj }"
  10. :style="{ 'max-height': testObj ? textHeight2 : '' }"
  11. >
  12. { { name }}
  13. </div>
  14. <span v-if="testObj" class="link2" @click="moreBtn()"></span>
  15. </div>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import { Col, Row } from "vant";
  21. export default {
  22. data() {
  23. return {
  24. textHeight2: "",
  25. testObj: false,
  26. name: "1啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊",
  27. };
  28. },
  29. components: {
  30. [Row.name]: Row,
  31. [Col.name]: Col,
  32. },
  33. mounted() {
  34. let that = this;
  35. this.testObj = false; // this.$set(this.testObj, "status2", ""); //1
  36. this.$nextTick(() => {
  37. that.calculateTextNew(); // DOM 加载完执行
  38. });
  39. window.onresize = () => {
  40. that.testObj = false; // this.$set(this.testObj, "status2", ""); //1
  41. setTimeout(() => {
  42. that.calculateTextNew(); //1
  43. }, 0);
  44. };
  45. },
  46. methods: {
  47. // 计算文字 显示展开 收起
  48. calculateTextNew() {
  49. let oneHeight = this.$refs.more2.scrollHeight; // 获取一行文字的height 计算当前文字比较列表文字
  50. let twoHeight = oneHeight * 2; // 2行文字高度
  51. this.textHeight2 = `${ twoHeight}px`; // 2行文本高度px
  52. let txtDom = this.$refs.textContainer2; //获取总文本
  53. let curHeight = txtDom.offsetHeight; // 获取总文本高度
  54. // 若总文本高度,大于两行文本高度 展示更多按钮 的样式
  55. if (curHeight > twoHeight) {
  56. this.testObj = true; // 展示 // this.$set(this.testObj, Object.assign(this.testObj, { status2: true }));
  57. } else {
  58. this.testObj = false; // 隐藏 // this.$set(this.testObj, Object.assign(this.testObj, { status2: null }));
  59. }
  60. },
  61. // 展开
  62. moreBtn() {
  63. this.testObj = !this.testObj;
  64. console.log(this.testObj.status2);
  65. },
  66. },
  67. };
  68. </script>
  69. <style lang="less" scoped>
  70. .list2 {
  71. padding: 5px 20px;
  72. border-bottom: 1px solid #eaeaea;
  73. position: relative;
  74. }
  75. .text2 {
  76. position: relative;
  77. color: #000;
  78. font-size: 14px;
  79. }
  80. .more2 {
  81. visibility: hidden;
  82. }
  83. .link2 {
  84. position: absolute;
  85. right: 30px;
  86. bottom: 14px;
  87. font-size: 12px;
  88. color: #2d95fe;
  89. width: 20px;
  90. height: 20px;
  91. transform: rotate(45deg);
  92. border-width: 0px 2px 2px 0px;
  93. border-color: red;
  94. border-style: solid;
  95. }
  96. .retract2 {
  97. position: relative;
  98. overflow: hidden;
  99. }
  100. .retract2:after {
  101. content: "...";
  102. position: absolute;
  103. bottom: 0;
  104. right: 2px;
  105. width: 60px;
  106. padding-left: 25px;
  107. background: linear-gradient(to right, transparent, #fff 45%);
  108. }
  109. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/76790
推荐阅读
相关标签
  

闽ICP备14008679号