首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >实现多行文本“展开收起” 超过一定行数隐藏/显示 Vue组件

实现多行文本“展开收起” 超过一定行数隐藏/显示 Vue组件

作者头像
拿我格子衫来
发布2022-01-24 15:16:13
发布2022-01-24 15:16:13
3.8K00
代码可运行
举报
文章被收录于专栏:TopFETopFE
运行总次数:0
代码可运行

vue组件,超过一定行数,将剩余内容隐藏,右下角显示...展开按钮。

info 要显示的文本内容 lineClamp 显示多少行 hiddenBtn 是有隐藏按钮,

代码语言:javascript
代码运行次数:0
运行
复制
<template>
  <div class="dp-text-ellipsis-wrapper">
    <div class="text" :class="textClass" :style="textStyleObject">
      <label class="btn" @click="showall = !showall"></label>
      {{ info }}
    </div>
  </div>
</template>
<script>
export default {
  name: 'DpTextEllipsis',
  props: {
    info: {
      type: String,
      default: '',
    },
    lineClamp: {
      type: Number,
      default: 3,
    },
    hiddenBtn: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      showall: false,
    }
  },
  computed: {
    textStyleObject() {
      return {
        'max-height': this.showall ? 'none' : `${1.5 * this.lineClamp}em`,
      }
    },
    textClass() {
      let cls = this.showall ? 'showall' : ''
      cls = cls + (this.hiddenBtn ? ' hidden-btn' : '')
      return cls
    },
  },
  watch: {
    info() {
      this.showall = false
    },
  },
}
</script>
<style lang="less">
.dp-text-ellipsis-wrapper {
  display: flex;
  margin: 6px 0 20px 0;
  overflow: hidden;
  font-size: 14px;
  line-height: 20px;

  .text {
    position: relative;
    overflow: hidden;
    line-height: 1.5;
    text-align: justify;
    text-overflow: ellipsis;
    word-break: break-all;
    transition: 0.3s max-height;
  }
  .text::before {
    float: right;
    height: calc(100% - 20px);
    content: '';
  }
  .text::after {
    position: absolute;
    width: 999vw;
    height: 999vw;
    margin-left: -100px;
    box-shadow: inset calc(100px - 999vw) calc(30px - 999vw) 0 0 #fff;
    content: '';
  }
  .btn {
    position: relative;
    float: right;
    clear: both;
    margin-left: 10px;
    font-size: 14px;
    padding: 0 8px;
    color: #206ef7;
    line-height: 20px;
    border-radius: 4px;
    cursor: pointer;
    z-index: 10;
  }
  .btn::after {
    /* stylelint-disable-next-line */
    font-family: element-icons !important;
    content: '展开\e790';
  }

  .text.showall {
    max-height: none;
  }
  .text.showall .btn::before {
    visibility: hidden;
  }
  .text.showall .btn::after {
    visibility: hidden;
  }
  .text.showall.hidden-btn .btn::after {
    content: '收起\e78f';
    visibility: visible;
  }
  .btn::before {
    position: absolute;
    left: 1px;
    color: #333;
    transform: translateX(-100%);
    content: '...';
  }
}
</style>

解决了 文章 https://juejin.cn/post/6963904955262435336

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档