内容折叠组件

内容折叠

1,为了方便复用,我们封装一个组件,直接上代码
HTML代码

  <div>
    <el-col :span="span" class="font-first" ref="content">{{content}}</el-col>
    <el-col :span="2">
      <el-button
        class="btn-detail float-right"
        type="text"
        @click="handleZoom"
      >
        {{btnName ? (btnName === 1 ? '收起' : '展开') : ''}}
      </el-button>
    </el-col>
  </div>

js代码

props: {
    content: {
      type: String,
      default: ''
    },
    isExpand: {
      type: Boolean,
      default: true
    },
    span: {
      type: Number,
      default: 19
    },
    // offset: {
    //   type: Boolean,
    //   default: false
    // }
  },
  data () {
    return {
      btnName: 0 // 1:收起,2:展开
    }
  },
 watch: {
    content: {
      immediate: true,
      handler: function (val) {
        if (val) {
          this.handleHeight()
        }
      }
    }
  },  
  methods: {
    handleHeight () {
      this.$nextTick(() => {
        if (this.content) {
          let height = window.getComputedStyle(this.$refs.content.$el).height
          if (height.includes('px') && +height.slice(0, -2) > 20) {
            this.btnName = this.isExpand ? 1 : 2
            if (!this.isExpand) {
              this.$refs.content.$el.classList.add('custom-cell')
            } else {
              this.$refs.content.$el.classList.remove('custom-cell')
            }
          } else {
            this.btnName = 0
          }
        }
      })
    },
    handleZoom () {
      this.$refs.content.$el.classList.toggle('custom-cell')
      this.btnName = this.btnName === 1 ? 2 : 1
    }
  }

css代码

.float-right {
  float: right;
}
.font-first {
  overflow: hidden;
  font-size: 14px;
  line-height: 20px;
}

这样就实现了一个内容折叠功能拉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值