vue封装一个modal组件

<template>
  <div class="container">
    <transition name="modal-fade">
      <div class="modal-container" v-show="visible">
        <!-- 头部标题 -->
        <div class="modal-header">
          <div class="modal-title">{{ title }}</div>
          <i class="iconfont close-icon" @click="close">&#xe7fc;</i>
        </div>
        <!-- 内容区域 -->
        <div class="modal-content">
          <slot></slot>
        </div>
        <!-- 底部按钮 -->
        <div class="modal-footer">
          <div class="modal-btn">
            <button class="cancel" ref="cancelBtn" @click="close" @mousedown="cancelMouseDown" @mouseup="cancelMouseUp">取消</button>
            <button class="ok" @click="ok">确认</button>
          </div>
        </div>
      </div>
    </transition>
    <!-- 遮罩层 -->
    <div class="mask" v-show="visible" @click="close"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },
  props: {
    // 模态框标题
    title: {
      type: String,
      default: () => {
        return '模态框标题'
      },
    },
    // 显示隐藏控件
    visible: {
      type: Boolean,
      default: () => {
        return false
      },
    },
  },
  methods: {
    // 取消
    close() {
      this.$emit('cancel')
      this.$emit('update:visible', false)
    },
    // 确认
    ok() {
      this.$emit('submit')
      this.$emit('update:visible', false)
    },
    // 取消按钮 鼠标按下事件
    cancelMouseDown() {
      this.$refs.cancelBtn.style.color = '#096dd9'
      this.$refs.cancelBtn.style.border = '1px solid #096dd9'
    },
    // 取消按钮 鼠标松开事件
    cancelMouseUp() {
      this.$refs.cancelBtn.style.color = '#595959'
      this.$refs.cancelBtn.style.border = '1px solid #d9d9d9'
    },
  },
  watch: {
    // 操作遮罩层的展示/隐藏
    visible() {
      if (this.visible == true) {
        document.querySelector('body').setAttribute('style', 'overflow:hidden !important;')
      } else {
        document.querySelector('body').removeAttribute('style')
      }
    },
  },
}
</script>

<style lang="less" scoped>
.modal-container {
  z-index: 999;
  background-color: #fff;
  min-width: 250px;
  min-height: 180px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border: none;
  border-radius: 4px;
  transition: 0.5s;
  // box-shadow: 5px 5px 5px #e4e4e4, 5px -5px 5px #e4e4e4, -5px 5px 5px #e4e4e4, -5px -5px 5px #e4e4e4;
  // 不需要知道宽高 水平垂直居中
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  // 头部标题
  .modal-header {
    width: 100%;
    height: 50px;
    border: none;
    border-bottom: 1px solid #e8e8e8;
    padding: 20px 30px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    // background-color: aquamarine;
    .modal-title {
      color: #262626;
      font-weight: 600;
      font-size: 17px;
    }
    .close-icon {
      color: #4d4d4d;
      cursor: pointer;
      width: 80px;
      text-align: right;
    }
  }
  // 内容
  .modal-content {
    width: 100%;
    min-height: 100px;
    border: none;
    border-radius: none;
    padding: 20px 30px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  // 底部按钮
  .modal-footer {
    width: 100%;
    height: 60px;
    border: none;
    border-top: 1px solid #e8e8e8;
    padding: 0 30px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    .modal-btn {
      width: 150px;
      display: flex;
      justify-content: space-between;
      .cancel {
        border: 1px solid #d9d9d9;
        background-color: #fff;
        color: #595959;
        width: 70px;
        height: 32px;
        border-radius: 4px;
        font-size: 14px;
        transition: 0.5s;
        &:hover {
          border: 1px solid #40a9ff;
          color: #40a9ff;
        }
      }
      .ok {
        border: 1px solid #1890ff;
        background-color: #1890ff;
        color: #ffffff;
        width: 70px;
        height: 32px;
        border-radius: 4px;
        font-size: 14px;
        transition: 0.5s;
        &:hover {
          border: 1px solid #40a9ff;
          background-color: #40a9ff;
        }
      }
    }
  }
}
// 遮罩层
.mask {
  z-index: 998;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  // opacity: 0.4 !important;
  // background: #000000 !important;
  background-color: rgba(0, 0, 0, 0.3);
}
// 模态框展示隐藏的动画
.modal-fade-enter-active {
  transition: all 0.3s ease;
}
.modal-fade-leave-active {
  transition: all 0.3s ease;
}
.modal-fade-enter-from,
.modal-fade-leave-to {
  transform: translateX(-400px);
  opacity: 0;
}
@font-face {
  font-family: 'iconfont'; /* Project id 3147059 */
  src: url('//at.alicdn.com/t/font_3147059_ypw5b7teii.woff2?t=1648289780095') format('woff2'), url('//at.alicdn.com/t/font_3147059_ypw5b7teii.woff?t=1648289780095') format('woff'),
    url('//at.alicdn.com/t/font_3147059_ypw5b7teii.ttf?t=1648289780095') format('truetype');
}
.iconfont {
  font-family: 'iconfont' !important;
  font-size: 18px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -webkit-text-stroke-width: 0.2px;
  -moz-osx-font-smoothing: grayscale;
}
</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值