【问题标题】:Vue Transitions: Sidebar transitioning in but not outVue 过渡:侧边栏过渡但不退出
【发布时间】:2021-08-02 09:17:44
【问题描述】:

我有一个侧边栏组件,它利用过渡元素在侧边栏隐藏和显示时将其滑入和滑出。关闭时它会滑出,但只要打开它,它就会弹出。

Sidebar.vue

<template>
  <aside v-if="mountSidebar" class="min-h-screen">
    <transition name="slide" appear @after-leave="close()">
      <div v-show="closeSidebar" class="min-h-screen" :class="`bg-${bgColor}`">
        <slot></slot>
      </div>
    </transition>
  </aside>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from "vue";

export default defineComponent({
  name: "Sidebar",
  props: {
    bgColor: {
      type: String,
      default: "blue",
    },
    textColor: {
      type: String,
      default: "white",
    },
    modelValue: {
      type: Boolean,
      default: true,
    },
  },
  emits: ["update:modelValue"],
  setup(props, { emit }) {
    const mountSidebar = ref(props.modelValue);
    const closeSidebar = ref(props.modelValue);

    const close = () => {
      emit("update:modelValue", false);
      mountSidebar.value = props.modelValue;
    };

    watch(
      () => props.modelValue,
      (value) => {
        if (value) mountSidebar.value = true;
        closeSidebar.value = value;
      }
    );

    return {
      close,
      mountSidebar,
      closeSidebar,
    };
  },
});
</script>

<style>
.slide-enter-active,
.slide-leave-active {
  transition: transform 0.2s ease;
}
.slide-enter,
.slide-leave-to {
  transform: translateX(-100%);
  transition: all 150ms ease-in 0s;
}
</style>

然后我的 App.vue 只有一个按钮,用于切换设置为侧边栏上的 modelValue 属性的值,即&lt;sidebar showSidebar ...&gt;content&lt;/sidebar&gt;

【问题讨论】:

    标签: vue.js css-transitions vuejs3 vue-transitions


    【解决方案1】:

    原来我一直是个大笨蛋,在使用 Vue 3 的同时阅读 Vue 2 的文档。修复它所需要的只是将 .slide-enter 更改为 .slide-enter-from

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2017-10-31
      • 2015-10-07
      • 1970-01-01
      • 2018-05-28
      • 2020-07-25
      相关资源
      最近更新 更多