当前位置:首页 > VUE

vue实现文本折叠

2026-01-17 00:04:22VUE

实现文本折叠的基本思路

文本折叠功能通常涉及控制文本的显示高度,超出指定行数后显示“展开/收起”按钮。Vue中可以通过动态绑定CSS样式和条件渲染实现这一效果。

使用CSS控制文本行数

通过CSS的-webkit-line-clamp属性限制文本显示行数,结合overflow: hidden实现折叠效果:

<template>
  <div>
    <div 
      :class="{'text-collapse': isCollapsed}" 
      ref="content"
    >
      {{ longText }}
    </div>
    <button @click="toggleCollapse">
      {{ isCollapsed ? '展开' : '收起' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      longText: '这里是需要折叠的长文本内容...',
      isCollapsed: true
    }
  },
  methods: {
    toggleCollapse() {
      this.isCollapsed = !this.isCollapsed
    }
  }
}
</script>

<style>
.text-collapse {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
</style>

动态计算是否需要折叠

对于不确定长度的文本,可通过计算文本高度决定是否需要显示折叠按钮:

<template>
  <div>
    <div 
      :style="{ height: isCollapsed ? maxHeight + 'px' : 'auto' }"
      ref="content"
      class="content"
    >
      {{ longText }}
    </div>
    <button 
      v-if="showToggle"
      @click="toggleCollapse"
    >
      {{ isCollapsed ? '展开' : '收起' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      longText: '长文本内容...',
      isCollapsed: true,
      showToggle: false,
      maxHeight: 60
    }
  },
  mounted() {
    this.checkOverflow()
  },
  methods: {
    toggleCollapse() {
      this.isCollapsed = !this.isCollapsed
    },
    checkOverflow() {
      const el = this.$refs.content
      this.showToggle = el.scrollHeight > el.clientHeight
    }
  }
}
</script>

<style>
.content {
  overflow: hidden;
  transition: height 0.3s ease;
}
</style>

使用第三方库

对于更复杂的需求,可以考虑使用专门处理文本折叠的Vue组件库:

  1. vue-text-collapse
    安装:npm install vue-text-collapse
    使用示例:

    <template>
      <text-collapse 
        :text="longText" 
        :length="100"
      />
    </template>
  2. clamp.js
    通过指令方式实现:

    import clamp from 'clamp-js'
    
    export default {
      directives: {
        clamp: {
          inserted(el, binding) {
            clamp(el, { clamp: binding.value || 3 })
          }
        }
      }
    }

    模板中使用:

    <div v-clamp>长文本内容...</div>

动画过渡效果

为折叠/展开添加平滑动画:

<template>
  <div>
    <div 
      class="content"
      :style="{ maxHeight: isCollapsed ? '60px' : 'none' }"
    >
      {{ longText }}
    </div>
    <button @click="toggleCollapse">
      {{ isCollapsed ? '展开' : '收起' }}
    </button>
  </div>
</template>

<style>
.content {
  overflow: hidden;
  transition: max-height 0.5s ease;
}
</style>

多行文本省略方案

纯CSS实现多行省略(兼容性方案):

vue实现文本折叠

.ellipsis {
  position: relative;
  line-height: 1.4em;
  max-height: 4.2em; /* 3行 */
  overflow: hidden;
}
.ellipsis::after {
  content: '...';
  position: absolute;
  right: 0;
  bottom: 0;
  background: white;
  padding: 0 5px;
}

标签: 文本vue
分享给朋友:

相关文章

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue使用vr实现标注

vue使用vr实现标注

Vue 中使用 VR 实现标注的方法 在 Vue 项目中结合 VR 技术实现标注功能,可以通过 WebXR 和 Three.js 等库实现。以下为具体实现方法: 安装依赖 确保项目中已安装 Thre…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…