当前位置:首页 > VUE

vue 实现div切换效果

2026-02-24 06:50:26VUE

使用 Vue 实现 Div 切换效果

方法一:通过 v-ifv-else 切换

通过 Vue 的指令 v-ifv-else 实现两个 div 的切换显示。

<template>
  <div>
    <button @click="toggleDiv">切换 Div</button>
    <div v-if="showDiv1">这是第一个 Div</div>
    <div v-else>这是第二个 Div</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showDiv1: true
    };
  },
  methods: {
    toggleDiv() {
      this.showDiv1 = !this.showDiv1;
    }
  }
};
</script>

方法二:通过 v-show 切换

v-show 通过 CSS 的 display 属性控制显示和隐藏,适合频繁切换的场景。

vue 实现div切换效果

<template>
  <div>
    <button @click="toggleDiv">切换 Div</button>
    <div v-show="showDiv1">这是第一个 Div</div>
    <div v-show="!showDiv1">这是第二个 Div</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showDiv1: true
    };
  },
  methods: {
    toggleDiv() {
      this.showDiv1 = !this.showDiv1;
    }
  }
};
</script>

方法三:动态组件切换

通过 Vue 的 <component> 动态组件实现多个 div 的切换。

vue 实现div切换效果

<template>
  <div>
    <button @click="currentComponent = 'Div1'">显示 Div1</button>
    <button @click="currentComponent = 'Div2'">显示 Div2</button>
    <component :is="currentComponent"></component>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentComponent: 'Div1'
    };
  },
  components: {
    Div1: {
      template: '<div>这是第一个 Div</div>'
    },
    Div2: {
      template: '<div>这是第二个 Div</div>'
    }
  }
};
</script>

方法四:通过 CSS 过渡效果增强切换

结合 Vue 的 <transition> 组件实现平滑的过渡效果。

<template>
  <div>
    <button @click="showDiv1 = !showDiv1">切换 Div</button>
    <transition name="fade">
      <div v-if="showDiv1" key="div1">这是第一个 Div</div>
      <div v-else key="div2">这是第二个 Div</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showDiv1: true
    };
  }
};
</script>

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

方法五:多内容切换(多个 div 切换)

通过数组和索引控制多个 div 的切换。

<template>
  <div>
    <button @click="nextDiv">下一个 Div</button>
    <div v-for="(content, index) in contents" :key="index" v-show="currentIndex === index">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      contents: ['Div 1 内容', 'Div 2 内容', 'Div 3 内容'],
      currentIndex: 0
    };
  },
  methods: {
    nextDiv() {
      this.currentIndex = (this.currentIndex + 1) % this.contents.length;
    }
  }
};
</script>

以上方法可以根据实际需求选择,v-if 适合条件渲染,v-show 适合频繁切换,动态组件适合复杂组件切换,过渡效果适合需要动画的场景。

标签: 效果vue
分享给朋友:

相关文章

vue 实现手册

vue 实现手册

以下是关于 Vue.js 实现手册的整理内容,涵盖核心概念、实践方法和常见场景的解决方案: 核心概念与基础用法 Vue.js 是一个渐进式 JavaScript 框架,核心特点包括数据驱动视图和组件…

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue 实现流程

vue 实现流程

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

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…