我有这个代码样本

家长:

<template>
  <div>
    <button @click="cartToggle">Toggle</button>
  </div>
  <div>
    <Child
      :visible="cartVisible"
      @update:visible="cartToggle"
      v-show="cartVisible"
    />
  </div>
</template>

<script setup>
import { ref } from "vue";
import Child from "./Child.vue";

const cartVisible = ref(false);
const cartToggle = () => {
  cartVisible.value = !cartVisible.value;
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
span {
  cursor: pointer;
}
</style>

子元素:

<script setup lang="ts">
import { toRefs, watchEffect, onBeforeUnmount } from "vue";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
});

const { visible } = toRefs(props);

const emit = defineEmits(["update:visible"]);

const hideCartBar = () => {
  emit("update:visible", false);
};

watchEffect(() => {
  document.body.style.overflow = visible.value ? "hidden" : "auto";
  document.body.style.paddingRight = visible.value ? "15px" : "0px";
});

onBeforeUnmount(() => {
  document.body.style.overflow = "auto";
  document.body.style.paddingRight = "0px";
});
</script>

<template>
  <div class="cartbar">
    <div class="cartbar-wrapper">
      <transition name="fade">
        <div
          v-show="props.visible"
          class="cartbar-overlay"
          @click="hideCartBar"
        ></div>
      </transition>
      <transition name="slide">
        <div v-show="props.visible" class="cartbar-container">
          <div class="cartbar__header">
            <h2>Menu</h2>
            <span @click="hideCartBar" class="cursor-pointer">Close</span>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<style scoped>
/* transition */
/* transition */
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.fade-enter-to,
.fade-leave-from {
  opacity: 1;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 500ms;
}

.slide-enter-from,
.slide-leave-to {
  transform: translateX(100%);
}

.slide-enter-to,
.slide-leave-from {
  transform: translateX(0);
}

.slide-enter-active,
.slide-leave-active {
  transition: transform 500ms;
}
/* cartbar */
.cursor-pointer {
  cursor: pointer;
}
.cartbar-wrapper {
  transition: visibility 0s 0s;
  z-index: 1300;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  width: 100%;
  padding-left: calc(4px * 9);
}

.cartbar-overlay {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.7);
  opacity: 1;
  cursor: pointer;
}
.cartbar-container {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 460px;
  height: 100%;
  overflow: hidden;
  margin-left: auto;
  padding: calc(4px * 7);
  background-color: #f4f2f1;
  color: #706b5f;
}
</style>

问题是,当我想切换菜单时,它可以很好地滑进go ,但不会滑出来加上-覆盖层根本没有动画效果

我做错了什么?

示例:https://codesandbox.io/p/sandbox/vue-3-glziy

推荐答案

子元素上的v-show="cartVisible"甚至在转出转换开始之前就删除了整个元素.但当简单地移除v-show时,.cartbar-wrapper将覆盖整个屏幕,使其无法按下按钮.您必须更改子组件的 struct ,以便它在没有容器的情况下工作.

一个简单的解决方案是,只需取下包装纸,将position:fixed放在.cartbar-container上,看看playground.

Vue.js相关问答推荐

具有VUE身份验证的Azure AD

Vue 3使用计算(computed)属性对象获取图像信息,在模板中呈现值时变为NaN

Vue3如何将自定义事件绑定到路由视图中的特定组件?

如何使用 vuejs 在浏览器 url 中获取当前 product.id 的详细信息

如何在vue中获取特定类别的产品列表

Vuex Mutations 和 Airbnb eslint

Vuetify 置顶工具栏

Vue 3 - 带有全局组件的无法解析组件

如何使用 vuex 删除项目?

Vue代理设置不起作用

Select 下拉列表中的复选框后如何清除 v-autocomplete(多个)搜索输入?

Vue 组件中的组件渲染函数中可能存在无限更新循环警告

我应该将所有数据存储在 vuex 状态

如何在 NuxtJS 中设置全局 $axios 标头

单击时交换组件

使用 vuejs 动态设置 id 标签

Vue-i18n - 无法读取未定义的属性配置

使用 svelte js 的原因

在 vue.js 应用程序中将静态内容(例如国家代码)存储在哪里?

在另一个 props 的验证器中访问 props 值