当前位置:首页 > VUE

Vue查看全文实现

2026-02-10 22:03:27VUE

Vue 查看全文实现方法

使用 v-ifv-else 控制显示

通过 v-ifv-else 指令切换显示全文和部分内容。定义一个布尔值变量 isExpanded 控制显示状态。

<template>
  <div>
    <p v-if="isExpanded">{{ fullText }}</p>
    <p v-else>{{ truncatedText }}</p>
    <button @click="isExpanded = !isExpanded">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullText: '这里是完整的文本内容,可能很长很长...',
      isExpanded: false
    };
  },
  computed: {
    truncatedText() {
      return this.fullText.slice(0, 50) + '...';
    }
  }
};
</script>

使用 CSS 控制文本溢出

通过 CSS 的 text-overflowwhite-space 属性实现文本截断,结合 Vue 动态切换类名。

<template>
  <div>
    <p :class="{ 'truncate': !isExpanded }">{{ fullText }}</p>
    <button @click="isExpanded = !isExpanded">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullText: '这里是完整的文本内容,可能很长很长...',
      isExpanded: false
    };
  }
};
</script>

<style>
.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>

使用第三方库 vue-truncate-collapsed

Vue查看全文实现

对于更复杂的需求,可以使用第三方库如 vue-truncate-collapsed 实现更灵活的截断和展开功能。

安装库:

npm install vue-truncate-collapsed

使用示例:

Vue查看全文实现

<template>
  <div>
    <truncate :length="50" :text="fullText" />
  </div>
</template>

<script>
import Truncate from 'vue-truncate-collapsed';

export default {
  components: { Truncate },
  data() {
    return {
      fullText: '这里是完整的文本内容,可能很长很长...'
    };
  }
};
</script>

动态计算截断位置

根据容器宽度动态计算截断位置,适用于响应式布局。

<template>
  <div ref="container">
    <p ref="text">{{ fullText }}</p>
    <button v-if="needsTruncation" @click="isExpanded = !isExpanded">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullText: '这里是完整的文本内容,可能很长很长...',
      isExpanded: false,
      needsTruncation: false
    };
  },
  mounted() {
    this.checkTruncation();
    window.addEventListener('resize', this.checkTruncation);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.checkTruncation);
  },
  methods: {
    checkTruncation() {
      const container = this.$refs.container;
      const text = this.$refs.text;
      this.needsTruncation = text.scrollWidth > container.offsetWidth;
    }
  }
};
</script>

结合过渡动画

为展开和收起添加过渡效果,提升用户体验。

<template>
  <div>
    <transition name="fade">
      <p v-if="isExpanded" key="full">{{ fullText }}</p>
      <p v-else key="truncated">{{ truncatedText }}</p>
    </transition>
    <button @click="isExpanded = !isExpanded">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
  </div>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

以上方法可以根据具体需求选择或组合使用,实现灵活的查看全文功能。

标签: 全文Vue
分享给朋友:

相关文章

Vue 实现登出功能

Vue 实现登出功能

实现登出功能的方法 在Vue中实现登出功能通常涉及清除用户认证信息、重置应用状态并跳转到登录页面。以下是几种常见的实现方式: 清除本地存储的token localStorage.removeIte…

Vue echarts实现散点图

Vue echarts实现散点图

Vue ECharts 实现散点图 安装 ECharts 在 Vue 项目中安装 ECharts 依赖: npm install echarts --save 引入 ECharts 在组件中引入 E…

Vue实现ui界面

Vue实现ui界面

Vue实现UI界面的方法 使用Vue CLI创建项目 通过Vue CLI快速搭建项目基础结构,确保开发环境配置完整。安装完成后运行vue create project-name选择预设配置或手动配置特…

Vue拖拽怎么实现

Vue拖拽怎么实现

Vue拖拽实现方法 使用Vue实现拖拽功能可以通过原生HTML5的拖拽API或第三方库如vuedraggable来实现。以下是两种常见的方法: 使用HTML5拖拽API HTML5提供了原生的拖拽…

vue展开全文实现

vue展开全文实现

Vue 实现展开全文功能 在 Vue 中实现展开全文功能可以通过动态绑定 class 或 style 来控制文本的显示高度,结合点击事件切换状态。以下是几种常见实现方式: 基础实现方案 使用 v-…

Vue 实现下载

Vue 实现下载

Vue 实现文件下载的方法 前端下载(纯前端生成文件) 适用于需要动态生成文件内容并下载的场景,如导出表格数据为 CSV 或 Excel。 // 创建 Blob 对象并触发下载 const d…