当前位置:首页 > VUE

vue实现收缩表单

2026-01-18 10:31:38VUE

Vue 实现收缩表单的方法

使用 v-show 或 v-if 控制显示

通过 Vue 的指令 v-showv-if 可以轻松实现表单的收缩效果。v-show 通过 CSS 的 display 属性控制显示,适合频繁切换的场景;v-if 会直接销毁和重建 DOM,适合不频繁切换的场景。

<template>
  <div>
    <button @click="toggleForm">Toggle Form</button>
    <form v-show="isFormVisible">
      <input type="text" placeholder="Name">
      <input type="email" placeholder="Email">
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isFormVisible: false
    }
  },
  methods: {
    toggleForm() {
      this.isFormVisible = !this.isFormVisible
    }
  }
}
</script>

结合 CSS 过渡动画

通过 Vue 的 <transition> 组件可以实现平滑的展开和收缩动画效果。

<template>
  <div>
    <button @click="toggleForm">Toggle Form</button>
    <transition name="slide">
      <form v-show="isFormVisible">
        <input type="text" placeholder="Name">
        <input type="email" placeholder="Email">
      </form>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isFormVisible: false
    }
  },
  methods: {
    toggleForm() {
      this.isFormVisible = !this.isFormVisible
    }
  }
}
</script>

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.5s ease;
}
.slide-enter, .slide-leave-to {
  max-height: 0;
  overflow: hidden;
}
.slide-enter-to, .slide-leave {
  max-height: 500px;
}
</style>

动态调整表单高度

通过动态绑定样式,可以实现更灵活的收缩效果,比如调整高度或宽度。

<template>
  <div>
    <button @click="toggleForm">Toggle Form</button>
    <form :style="{ height: isFormVisible ? 'auto' : '0', overflow: 'hidden' }">
      <input type="text" placeholder="Name">
      <input type="email" placeholder="Email">
    </form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isFormVisible: false
    }
  },
  methods: {
    toggleForm() {
      this.isFormVisible = !this.isFormVisible
    }
  }
}
</script>

使用第三方库

如果需要更复杂的动画效果,可以结合第三方动画库如 animate.css

<template>
  <div>
    <button @click="toggleForm">Toggle Form</button>
    <transition
      enter-active-class="animate__animated animate__fadeInDown"
      leave-active-class="animate__animated animate__fadeOutUp"
    >
      <form v-show="isFormVisible">
        <input type="text" placeholder="Name">
        <input type="email" placeholder="Email">
      </form>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isFormVisible: false
    }
  },
  methods: {
    toggleForm() {
      this.isFormVisible = !this.isFormVisible
    }
  }
}
</script>

表单数据的处理

在收缩表单时,可能需要清除表单数据或保留数据。可以通过 v-model 绑定数据,并在适当的时候重置。

<template>
  <div>
    <button @click="toggleForm">Toggle Form</button>
    <form v-show="isFormVisible">
      <input type="text" v-model="formData.name" placeholder="Name">
      <input type="email" v-model="formData.email" placeholder="Email">
    </form>
    <button @click="resetForm" v-show="isFormVisible">Reset</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isFormVisible: false,
      formData: {
        name: '',
        email: ''
      }
    }
  },
  methods: {
    toggleForm() {
      this.isFormVisible = !this.isFormVisible
    },
    resetForm() {
      this.formData = {
        name: '',
        email: ''
      }
    }
  }
}
</script>

以上方法可以根据实际需求选择适合的方式实现表单的收缩效果。

vue实现收缩表单

标签: 表单vue
分享给朋友:

相关文章

vue实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Card…

vue实现组件循环

vue实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue播放倍速怎么实现

vue播放倍速怎么实现

实现Vue播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取…

vue实现a4打印模板

vue实现a4打印模板

使用Vue实现A4打印模板 在Vue中实现A4打印模板需要结合CSS的打印样式和Vue的模板渲染能力。以下是具体实现方法: 设置A4纸张尺寸 在CSS中定义A4纸张的标准尺寸(210mm × 29…