固定高度然后超出的内容,让高度自动随着内容增加而增加

  • 利用scrollHeight,获取元素的实际高度,然后进行判断,动态设置元素的高度度
  • 这个例子是更多和收起例子
<template>
  <div class="subject bg_border" ref="parent" :style="{height : height + 'px'}">
    <div class="subject_title">
      <span>{{title}}</span>
      <p class="more" @click="unfold" v-show="text">{{ isClickMore ? "收起" : "更多"}}</p>
    </div>

    <div class="btn_gather clearFix" @click="(e)=>{getChildrenElement(e , cls , type)}">
      <div
        class="btn_item"
        v-for=" (item , index) in doMain"
        :key="item.name"
        :data-index="index"
        :data-id="item.id"
      >
        <span >{{item.name}}</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name : "fold",
  data() {
    return {
      isClickMore: false
    };
  },
  props: {
    title: String,
    doMain: Array,
    cls: String,
    type: Number,
    text: {
      type: Boolean,
      default: true
    },
    height : {
      type : Number,
      default : 120
    }
  },
  methods: {
    unfold(e) {
      this.isClickMore = !this.isClickMore;
      let parent = this.$refs.parent;
      // 获取元素实际需要占据的高度
      let height = parent.scrollHeight
      // 判断是收起还是更多,动态设置高度
      this.isClickMore
        ? parent.style.setProperty("height", height + 'px')
        : parent.style.setProperty("height", this.height + 'px');
    },
    getChildrenElement(e, className, type) {
      this.$emit("getChildrenElement", {
        e,
        className,
        type
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.subject {
  width: 100%;
  box-sizing: border-box;
  padding: 0 10px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease-out;
  .subject_title {
    height: 28px;
    font-size: 13px;
    font-weight: 600;
    color: #000;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
     
    p {
      color: grey;
    }
  }
  .btn_gather {
    // height: 100px;
    //   overflow: hidden;
    .btn_item {
      width: 84px;
      height: 34px;
      margin-bottom: 8px;
      text-align: center;
      line-height: 34px;
      border: 2px solid #e8e8e8;
      border-left: 1px solid #e8e8e8;
      border-right: 1px solid #e8e8e8;
      margin-right: 10px;
      color: #4c4c4c;
      font-size: 12px;
      position: relative;
      float: left;
      text-justify: newspaper;
      word-break: break-all;

      span {
        pointer-events: none;
        position: absolute;
        display: block;
        width: 100%;
        text-align: center;
        line-height: 34px;
        height: 100%;
      }
    }
    .btn_item_active::after {
      content: "";
      border: 10px solid #4285f4;
      border-color: transparent #4285f4 #4285f4 transparent;
      position: absolute;
      right: 0;
      top: 39%;
    }
    .btn_item_active::before {
      content: "";
      width: 7px;
      height: 3px;
      border: 1px solid #fff;
      border-color: transparent transparent #fff #fff;
      position: absolute;
      right: 2px;
      top: 68%;
      z-index: 3;
      transform: rotate(-35deg);
    }
  }
}
.over_subject {
  height: auto !important;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值