VUE Dialog

代码积累: VueJs 下基于 Slot 方式的 「淡入淡出」弹框,最最基础的功能

<template>
  <div
    :class="['dialog-mask', visible && 'fade-in']"
    @click="close"
  >
    <div
      class="dialog-content"
      @click.stop=""
    >
      <slot />
    </div>
  </div>
</template>

<script>
export default {
  name: 'prize-dialog',
  data () {
    return {
      visible: false
    }
  },
  mounted () {
    const body = document.querySelector('body')
    if (body.append) {
      body.append(this.$el)
    } else {
      body.appendChild(this.$el)
    }
  },
  methods: {
    open () {
      this.visible = true
    },
    close () {
      this.visible = false
    }
  }
}
</script>

<style scoped lang="scss">
  .dialog-mask {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background: rgba(0, 0, 0, 0.19);
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s;
  }

  .dialog-content {
    background: transparent;
    min-height: 1rem;
    min-width: 1rem;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate3d(-50%, -50%, 0) scale(.7);
    transform-origin: center;
    opacity: 0;
    transition: all 0.3s;
  }

  .fade-in {
    visibility: visible;
    opacity: 1;
    .dialog-content {
      transform-origin: center;
      transform: translate3d(-50%, -50%, 0) scale(1);
      opacity: 1;
    }
  }
</style>