modal.vue 2.44 KB
<template>
  <div class="modal-box" v-show="value" v-transfer-dom :data-transfer="transfer">
    <div class="modal-mask"></div>
    <div class="modal-wrap" @touchmove.prevent.stop="onTouchmove">
      <div class="modal modal-content">
        <div class="modal-body">
          <slot>
            <div class="text">{{title}}</div>
          </slot>
        </div>
        <div class="modal-footer">
          <slot name="footer">
            <Button class="btn" :class="{active: loading}" type="button" @click="onSure">{{sureText}}</Button>
            <Button class="btn" type="button" @click="onCancel">{{cancelText}}</Button>
          </slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Button from 'components/button2.vue';

export default {
  name: 'Modal',
  props: {
    transfer: [Boolean],
    title: String,
    value: Boolean,
    loading: Boolean,
    sureText: {
      type: String,
      default: '确认'
    },
    cancelText: {
      type: String,
      default: '取消'
    }
  },
  methods: {
    onTouchmove() {},
    onCancel() {
      this.$emit('on-cancel');
      this.$emit('input', false);
    },
    onSure() {
      if (!this.loading) {
        this.$emit('on-sure');
      }
    }
  },
  components: {Button}
};
</script>

<style lang="scss">
.modal-box {
  font-size: 24px;
}

.modal-mask {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.4);
  height: 100%;
  z-index: 99;

  &-hidden {
    display: none;
  }
}

.modal {
  width: auto;
  margin: 0 auto;
  position: relative;
  outline: none;

  &-hidden {
    display: none !important;
  }

  &-wrap {
    position: fixed;
    overflow: auto;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 99;
    -webkit-overflow-scrolling: touch;
    outline: 0;
  }

  &-content {
    position: relative;
    background-color: #fff;
    top: 314px;
    border: 0;
    width: 600px;
    background-clip: padding-box;

    & .text {
      text-align: center;
      padding-top: 30px;
      padding-bottom: 30px;
    }
  }

  &-body {
    padding: 38px;
  }

  &-footer {
    width: 100%;
    border-top: 1px solid #eee;
    display: flex;

    button {
      width: 50%;
      overflow: hidden;
      height: 100px;
      background: none;

      &:first-child {
        color: #999;
      }
    }

    button + button {
      margin-left: 8px;
      margin-bottom: 0;
      border-left: 1px solid #eee;
    }
  }
}
</style>