美文网首页
关于如何实现多余内容展开和收起

关于如何实现多余内容展开和收起

作者: 辉夜真是太可爱啦 | 来源:发表于2023-09-17 10:02 被阅读0次

    效果如图:

    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:
            '立秋以后气温由热转凉,人体的消耗也逐渐减少,食欲开始增加。因此,可根据秋季的特点来科学地摄取营养和调',
        },
      },
    
      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>
    
    

    相关文章

      网友评论

          本文标题:关于如何实现多余内容展开和收起

          本文链接:https://www.haomeiwen.com/subject/rrxwvdtx.html