记录前端vue3封装一个modal弹框

06-01 1752阅读

一、基本步骤

  • 1、最终运行效果

    记录前端vue3封装一个modal弹框

  • 2、具体代码实现

      
        
          
            
            
              
                
                  
                    
                  
                  {{ title }}
                  x
                
                
                  
                
                
                  
                
              
            
          
        
      
    
    
    import { computed, defineProps, nextTick, ref, watch ,useSlots} from 'vue';
    const isHeader = !!useSlots().title;
    const isFooter = !!useSlots().footer;
    const props = defineProps({
      title: {
        type: String,
        default: '',
      },
      visible: {
        type: Boolean,
        required: true,
      },
      width: {
        type: Number,
        default: 480,
      },
      closeButton: {
        type: Boolean,
        default: true,
      },
      closeOnClickMask: {
        type: Boolean,
        default: true,
      },
      closeOnEsc: {
        type: Boolean,
        default: true,
      },
      contentStyle: {
        type: Object,
        default: () => ({}),
      },
    });
    const modalRef = ref();
    const emit = defineEmits(['update:visible', 'closed']);
    const contentVisible = ref(false);
    const contentStyle = computed(() => {
      return {
        width: props.width + 'px',
        ...(props.contentStyle || {}),
      };
    });
    watch(
      () => props.visible,
      () => {
        if (props.visible) {
          nextTick(() => modalRef.value.focus());
        }
      }
    );
    const close = () => {
      emit('update:visible', false);
      emit('closed');
    };
    const onEsc = () => {
      if (props.visible && props.closeOnEsc) close();
    };
    const onClickMask = () => {
      if (props.closeOnClickMask) close();
    };
    
    
    .modal,
    .mask {
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 5000;
    }
    .modal {
      position: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      outline: 0;
      border: 0;
    }
    .mask {
      position: absolute;
      background: rgba(0, 0, 0, 0.25);
    }
    .modal-content {
      z-index: 5001;
      background: #fff;
      border-radius: 2px;
      overflow: hidden;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
      .modal-header {
        width: 100%;
        height: 48px;
        padding: 0 20px;
        box-sizing: border-box;
        font-size: 16px;
        font-weight: 500;
        color: #1d2129;
        line-height: 48px;
        border-bottom: 1px solid #e5e6eb;
        position: relative;
        display: flex;
        flex-direction: row;
        align-items: center;
        .close-btn {
          width: 20px;
          height: 20px;
          display: flex;
          justify-content: center;
          align-items: center;
          position: absolute;
          top: 16px;
          right: 16px;
          cursor: pointer;
        }
      }
      .modal-body {
        padding: 20px;
        box-sizing: border-box;
      }
      .modal-footer {
        border-top: 1px solid #e5e6eb;
        padding: 16px 20px;
        display: flex;
        flex-direction: row;
        justify-content: flex-end;
        align-items: center;
      }
    }
    .modal-fade-enter-active {
      animation: modal-fade-enter 0.25s both ease-in;
    }
    .modal-fade-leave-active {
      animation: modal-fade-leave 0.25s both ease-out;
    }
    .modal-zoom-enter-active {
      animation: modal-zoom-enter 0.25s both cubic-bezier(0.4, 0, 0, 1.5);
    }
    .modal-zoom-leave-active {
      animation: modal-zoom-leave 0.25s both;
    }
    @keyframes modal-fade-enter {
      from {
        opacity: 0;
      }
    }
    @keyframes modal-fade-leave {
      to {
        opacity: 0;
      }
    }
    @keyframes modal-zoom-enter {
      from {
        transform: scale3d(0.3, 0.3, 0.3);
      }
    }
    @keyframes modal-zoom-leave {
      to {
        transform: scale3d(0.3, 0.3, 0.3);
      }
    }
    
    
  • 3、使用组件的时候

      你好
       

    你好

    关闭
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

相关阅读

目录[+]

取消
微信二维码
微信二维码
支付宝二维码