Vue项目实现折叠,下面半透明的效果

Vue项目实现折叠,下面半透明的效果

效果图

在这里插入图片描述

实现思路:文字收缩的时候,给它下面添加一个渐变的遮罩层,当展开的时候,把那个遮罩层给去掉。

页面代码
<template>
      <div class="question-description"  ref="hei">
        <div class="rich-text">{{ message }}</div>
        <div :class="!isshow ? 'question-mask' : ''"></div>
      </div>
    <el-button @click="show">{{ isshow ? "收起" : "展开" }}</el-button>
  </div>
</template>
data() {
    return {
      isshow: false,
    };
  },
js代码
show() {
      this.isshow = !this.isshow;
      if(this.isshow){
        this.$refs.hei.style.maxHeight = ""
      }else{
        this.$refs.hei.style.maxHeight = "5.2em"
      }
    },
css代码(关键代码)
body {
  color: #555;
  font-size: 16px;
  line-height: 1.7;
  word-wrap: break-word;
  font-family: "Microsoft Yahei";
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
.question-description {
  position: relative;
  min-height: 5.1em;
  margin-top: 5px;
  overflow: hidden;
  width: 370px;
}
.question-description.is-expanded {
  max-height: none;
}
.question-mask {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  background-image: -webkit-linear-gradient(top, hsla(0, 0%, 100%, 0.5), #fff);
  background-image: linear-gradient(180deg, hsla(0, 0%, 100%, 0.5), #fff);
}
.expandBtn {
  position: absolute;
  right: 0;
  bottom: 0;
  padding: 4px 0 0 5px;
  color: #999;
  background-color: #fff;
  cursor: pointer;
  line-height: 1.5;
  border-radius: 4px;
  border: 0 none;
  font-size: 16px;
}
特别注意:

要在生命周期的mounted中,先给显示内容的容器一个高度,要不就会出现刚进来时内容全部显示的问题,比如下图
在这里插入图片描述
所以在页面挂载之后,给他一个最小高度

mounted(){
    this.$refs.hei.style.maxHeight = "5.2em"
  },

原文在这里

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值