当前位置:首页 > 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 属性控制显示和隐藏,适合频繁切换的场景。

<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 的切换。

<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 的切换。

vue 实现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实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…

vue下载实现

vue下载实现

Vue 下载实现方法 在 Vue 项目中实现文件下载功能可以通过多种方式完成,以下是几种常见的方法: 使用 <a> 标签实现下载 通过动态创建 <a> 标签并设置 href…

vue实现项目

vue实现项目

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 创建 Vue 项目 使用 Vue CLI 快…

vue实现登录认证

vue实现登录认证

Vue 登录认证实现方法 使用 Vue Router 和 Vuex 管理登录状态 在 Vue 项目中,通常使用 Vue Router 进行路由管理,Vuex 进行状态管理。登录认证的核心是验证用户身份…

vue实现拦截登录

vue实现拦截登录

Vue 实现登录拦截的方法 使用路由守卫进行拦截 Vue Router 提供了全局前置守卫 beforeEach,可以在路由跳转前进行验证。在路由配置文件中添加以下代码: import router…