JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Expand and collapse text in Vue

Author:JIYIK Last Updated:2025/04/20 Views:

With the rapid development of mobile Internet, users have higher and higher requirements for page interactivity and user experience. The functions of expanding and collapsing text are becoming more and more common in page design, which can improve the readability and aesthetics of the page. This article will introduce how to implement the functions of expanding and collapsing text in Vue.


1. Demand Analysis

Before implementing the text expansion and collapse function, we need to analyze the requirements. For the text expansion and collapse function, we need to consider the following aspects:

  1. When the text length exceeds a certain range, an expand/collapse button needs to be displayed.
  2. In the initial state, only text of a certain length is displayed. Click the expand button to display all the text, and click the collapse button again to hide the redundant text.
  3. The expanded/collapsed state needs to be saved, that is, the last state should be maintained when the user opens the page again.

2. Implementation ideas

Based on the demand analysis, we can come up with the implementation ideas:

  1. Use v-ifthe command to determine whether the text length exceeds a certain range. If so, display the expand/collapse button.
  2. Use v-showthe command to control the display and hiding of text. In the initial state, only text of a certain length is displayed. Click the expand button to display all the text, and click the collapse button again to hide the extra text.
  3. Use Vuethe lifecycle hook functions mounted and updated to save the expanded/collapsed state.

3. Implementation steps

Use the directive in the template v-ifto determine whether the text length exceeds a certain range. If so, display the expand/collapse button.

<template>
  <div>
    <p v-if="showBtn">{{ content }}</p>
    <button v-if="showBtn" @click="toggleContent">
      {{ isCollapsed ? '展开' : '收起' }}
    </button>
  </div>
</template>

Define variables in data to save the expanded/collapsed state of the text. Update the state in the mounted and updated lifecycle hook functions.

<script>
export default {
  data() {
    return {
      content: '这是一段很长的文字,需要展开和收起。',
      maxLength: 20,
      isCollapsed: true,
      showBtn: false,
    };
  },
  mounted() {
    this.showBtn = this.content.length > this.maxLength;
  },
  updated() {
    this.showBtn = this.content.length > this.maxLength;
  },
  methods: {
    toggleContent() {
      this.isCollapsed = !this.isCollapsed;
    },
  },
};
</script>

Use v-showthe command to control the display and hiding of text.

<template>
  <div>
    <p v-if="showBtn" v-show="isCollapsed">{{ content.substring(0, maxLength) }}...</p>
    <p v-if="showBtn" v-show="!isCollapsed">{{ content }}</p>
    <button v-if="showBtn" @click="toggleContent">
      {{ isCollapsed ? '展开' : '收起' }}
    </button>
  </div>
</template>

4. Example Demonstration

Here is a complete example code, which can be viewed on CodePen.

<template>
  <div>
    <p v-if="showBtn" v-show="isCollapsed">{{ content.substring(0, maxLength) }}...</p>
    <p v-if="showBtn" v-show="!isCollapsed">{{ content }}</p>
    <button v-if="showBtn" @click="toggleContent">
      {{ isCollapsed ? '展开' : '收起' }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '这是一段很长的文字,需要展开和收起。',
      maxLength: 20,
      isCollapsed: true,
      showBtn: false,
    };
  },
  mounted() {
    this.showBtn = this.content.length > this.maxLength;
  },
  updated() {
    this.showBtn = this.content.length > this.maxLength;
  },
  methods: {
    toggleContent() {
      this.isCollapsed = !this.isCollapsed;
    },
  },
};
</script>

V. Conclusion

The function of expanding and collapsing text can improve the readability and aesthetics of the page, and is one of the common functions in page design. To implement the function of expanding and collapsing text in Vue, we need to analyze the requirements first, and then come up with implementation ideas based on the requirements analysis. Finally, we can use v-ifthe directive to determine whether the length of the text exceeds a certain range, use v-showthe directive to control the display and hiding of the text, and use Vue's life cycle hook functions mounted and updated to save the expansion/collapse status.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Detailed explanation of Vue life cycle hooks

Publish Date:2025/04/20 Views:60 Category:Vue

Like other frameworks, Vue has a number of lifecycle hooks that allow us to attach code to specific events that occur when creating or using a Vue application — for example, when a component loads, when a component is added to the DOM, or

How to use MUI methods in Vue

Publish Date:2025/04/20 Views:141 Category:Vue

MUI is a front-end framework based on HTML5 and CSS3. It provides a series of UI components and JS plug-ins to help developers quickly build beautiful and smooth mobile web applications. Using MUI in Vue can further improve development effi

Implementing paging in Vue

Publish Date:2025/04/20 Views:196 Category:Vue

Pagination is one of the common features in modern web applications. It allows users to browse large amounts of data by dividing the data into multiple pages. In Vue, we can use some libraries and components to implement pagination. In this

Setting up a countdown timer in Vue

Publish Date:2025/04/20 Views:194 Category:Vue

Countdown timers are a very common feature in modern web applications. In Vue, we can easily implement a countdown timer to provide feedback to users when they perform certain actions. In this article, we'll look at how to set up a countdow

Import JSON file into Vue

Publish Date:2025/04/20 Views:188 Category:Vue

Vue.js is a popular JavaScript framework that can quickly build single-page applications (SPA). In Vue.js, we can use JSON files to store data, which is very useful in development. This article will introduce how to introduce JSON files in

Different ways to send a POST request in Vue

Publish Date:2025/04/20 Views:151 Category:Vue

In Vue, sending a POST request is a very common operation, which can be achieved in many ways. This article will introduce several methods of sending a POST request in Vue, and will explain it in detail with examples. Method 1: Using Vue-re

Dynamically add properties and values to objects in Vue

Publish Date:2025/04/20 Views:199 Category:Vue

In Vue, we often need to manipulate the properties and values ​​of objects. Sometimes we need to dynamically add properties and values ​​to meet business needs. This article will introduce in detail how to dynamically add properties

How to scroll to the top or bottom of the page in Vue.js

Publish Date:2025/03/01 Views:96 Category:Vue

Vue.js is a popular front-end framework that helps developers build efficient and maintainable applications. In Vue.js, scrolling the page to the top or bottom is a common requirement. In this article, we will introduce how to implement this

Display element or text on mouse hover in vue

Publish Date:2025/03/01 Views:182 Category:Vue

Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial