当前位置:首页 > 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 中实现手机推送的方法 在 Vue 项目中实现手机推送功能,通常需要结合第三方推送服务(如 Firebase Cloud Messaging、极光推送等)或原生移动端能力(通过 Cordova/…

Vue全局loading实现

Vue全局loading实现

Vue全局Loading的实现方法 使用Vue插件封装全局Loading组件 创建一个独立的Loading组件,通过Vue插件机制注册为全局组件或方法。 <template> &…

Vue实现时钟

Vue实现时钟

Vue实现时钟的方法 使用Vue实现时钟可以通过多种方式完成,以下是几种常见的实现方法: 使用Date对象和setInterval 通过JavaScript的Date对象获取当前时间,并使用setI…

Vue实现整体缩小

Vue实现整体缩小

Vue实现整体缩放的常见方法 使用CSS transform属性 在Vue组件的样式中添加transform属性可以实现整体缩放效果。这种方法不会影响页面布局,仅改变视觉呈现。 .scale-con…

Vue实现抠图

Vue实现抠图

Vue实现抠图的方法 使用HTML5 Canvas和Fabric.js 在Vue项目中集成Fabric.js库可以实现前端抠图功能。Fabric.js提供了丰富的图像处理API,支持交互式抠图操作。…

使用Vue实现洗牌动画

使用Vue实现洗牌动画

使用CSS和Vue实现洗牌动画 洗牌动画可以通过CSS过渡和Vue的动态数据绑定实现。以下是实现步骤: 准备数据 data() { return { cards: [ {…