關(guān)于如何實現(xiàn)多余內(nèi)容展開和收起

效果如圖:

image.png
image.png
<template>
  <div class="expend-content-main-box">
    <div class="content" v-if="content.length > 21">
      <span v-if="!isShowAll">{{ content.slice(0, 18) }}...</span>
      <span v-else>{{ content }}</span>
      <div class="arrow-box" v-if="content.length > 21" @click="showAll">
        <span v-if="isShowAll">收起</span>
        <span v-else>展開</span>
        <img
          src="../assets/arrow.png"
          :style="{ transform: isShowAll ? 'rotate(-180deg)' : 'rotate(0deg)' }"
          alt=""
        />
      </div>
    </div>
    <div class="content" v-else>
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  name: 'expendContent',

  props: {
    content: {
      type: String,
      default:
        '立秋以后氣溫由熱轉(zhuǎn)涼,人體的消耗也逐漸減少,食欲開始增加。因此,可根據(jù)秋季的特點來科學地攝取營養(yǎng)和調(diào)',
    },
  },

  data() {
    return {
      isShowAll: false,
    }
  },

  methods: {
    showAll() {
      this.isShowAll = !this.isShowAll
    },
  },
}
</script>

<style lang="less" scoped>
.expend-content-main-box {
  width: 100%;
  display: flex;
  justify-content: space-between;

  .content {
    width: 100%;
    font-weight: 400;
    font-size: 14px;
    color: #777777;
    letter-spacing: 0;
    white-space: pre-wrap;
    line-height: 24px;
  }

  .arrow-box {
    float: right;
    display: inline-flex;
    align-items: center;
    line-height: 24px;

    img {
      width: 10px;
      object-fit: contain;
      margin-left: 4px;
      transition: all 0.5s;
    }
  }
}
</style>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容