• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

vue3 모달창 트랜지션

24.01.30 15:44 작성 조회수 224

1

<template>
    <transition appear name="modal">
        <div class="modal-mask">
            <div class="modal-wrapper">
              <div class="modal-container">

                <!-- Modal Header -->
                <div class="modal-header">
                  <slot name="header">
                      default header
                  </slot>
                </div>

                <!-- Modal Body -->
                <div class="modal-body">
                  <slot name="body">
                      default body
                  </slot>
                </div>

                <!-- Modal footer -->
                <!-- <div class="modal-footer">
                  <slot name="footer">
                      default footer
                      <button class="modal-default-button" @click="$emit('close')">
                        OK
                      </button>
                  </slot>
                </div> -->
              </div>
            </div>
        </div>
    </transition>
</template>

<style scoped>
.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: table;
  transition: opacity 0.3s ease;
}

.modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}

.modal-container {
  width: 300px;
  margin: 0px auto;
  padding: 20px 30px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
  transition: all 0.3s ease;
  font-family: Helvetica, Arial, sans-serif;
}

.modal-header {
  margin-top: 0;
  color: #42b983;
}

.modal-body {
  margin: 20px 0;
}

.modal-default-button {
  float: right;
}

.modal-enter-from {
  opacity: 0;
}

.modal-leave-active {
  opacity: 0;
}

.modal-enter-from .modal-container,
.modal-leave-active .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}
</style>

vue3인데 모달창 띄울 때 애니매이션 효과가 잘 작동하는데 왜 닫을 때는 작동을 안하는 지 잘 모르겠어요

<AlertModal v-if="showModal" @close="showModal = false">
            <!--
            you can use custom content here to overwrite
            default content
            -->
            <template v-slot:header>
                <h3>경고! <span class="closeModalBtn" @click="showModal = false">x</span></h3>
            </template>
            <template v-slot:body>
                아무것도 입력하지 않으셨습니다.
            </template>
            <!-- <template v-slot:footer>
                copy right
            </template> -->
</AlertModal>

참고로 vue3에서는 slot을 template 태그 안에 v-slot으로 적어야 한다해서 이렇게 작성했어요

답변 2

·

답변을 작성해보세요.

0

안녕하세요, 코드 상으론 문제 없어 보여요. AI 답변 확인해 보시겠어요? :)

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.02.01

안녕하세요, 인프런 AI 인턴입니다.

우선 v-if는 조건이 false가 되는 순간 요소를 즉시 제거하기 때문에, Vue.js에서 모달창 닫기 시 애니메이션 효과가 작동하지 않는 이유로 생각됩니다. 트랜지션 효과가 보이려면 DOM에서 제거되기 전에 애니메이션이 실행될 수 있는 시간이 필요합니다.

대신 v-if대신 v-show를 사용하여 모달창 표시 상태를 제어하면, 요소는 계속 DOM에 남아있지만 보이거나 숨겨지는 형태가 됩니다. 이렇게 하면 트랜지션 효과가 정상적으로 작동할 수 있습니다.

또한, 트랜지션의 동작을 위해서는 modal-entermodal-leave 클래스명을 사용해야 합니다. 이미 modal-enter-frommodal-leave-active 스타일이 작성되어 있는데, name 속성 값인 “modal”에 따라 modal-enter-active, modal-enter-to, modal-leave-from, modal-leave-to 같은 클래스명을 사용해야 합니다.

모달이 닫힐 때도 트랜지션 효과가 작동하도록 하려면 다음과 같이 코드를 조정할 수 있습니다:

<AlertModal v-show="showModal" @close="showModal = false">
   <!-- 모달 콘텐츠 -->
</AlertModal>

그리고 <style> 내에 .modal-enter-active, .modal-leave-active 클래스를 아래와 같이 추가해주세요:

.modal-enter-active,
.modal-leave-active {
  transition: opacity 0.3s ease;
}
.modal-enter, .modal-leave-to /* .modal-leave-active in <2.1.8 */ {
  opacity: 0;
  transform: scale(1.1);
}

이렇게 수정하면 Vue 3에서 모달창이 닫힐 때도 애니메이션 효과가 잘 작동할 것입니다.