• Vue3+ts实现简单版本modal


    Vue3+ts实现简单版本modal

    项目中要求做一个询问弹框,有点类似ant-design-vue中的modal通过API调用的那种效果。虽然使用的ui库arco-design-vue中的modal也能满足我的需求,但是我懒得改样式,索性自己写一个阉割版的好了。
    实现的这个modal只支持通过API来进行调用

    结构组件

    <template>
      <div v-if="mask && visible" class="mask"></div>
      <div class="container">
        <div v-if="visible" class="confirm">
          <span class="close" @click="cancel">
            <IconClose />
          </span>
          <div ref="moveEl" :class="{ head: true, move: draggable }">
            <!-- 这里可以换成自己需要的图标或者图片 -->
            <img :src="url" alt="" />
            <span class="title">{{ title }}</span>
          </div>
          <div class="msg">{{ content }}</div>
          <div class="footer">
            <button class="cancel" @click="cancel">取消</button>
            <button class="sure" @click="ok">确认</button>
          </div>
        </div>
      </div>
    </template>
    <script lang="ts" setup>
      import { ref, onMounted, watch } from 'vue';
      import { IconClose } from '@arco-design/web-vue/es/icon';
      import url from '@/assets/icons/confirm-icon.png';
    
      const emits = defineEmits(['ok', 'cancel']);
      const moveEl = ref<HTMLElement | null>(null);
      const props = defineProps({
        visible: Boolean, // 是否展示弹框
        title: String,  // 标题
        content: String,  // 内容
        mask: Boolean,  // 是否展示蒙层
        draggable: Boolean, // 是否可拖拽
      });
    
      function ok() {
        emits('ok');
      }
    
      function cancel() {
        emits('cancel');
      }
    
      onMounted(() => {
        let dx = 0;
        let dy = 0;
        let sx = 0;
        let sy = 0;
        let box: HTMLElement | null = null;
        function move(ev: MouseEvent) {
          if (box) {
            let left = ev.clientX - (dx - sx);
            let top = ev.clientY - (dy - sy);
            if (left <= 0) left = 0;
            if (top <= 0) top = 0;
            if (left >= window.innerWidth - box.clientWidth) left = window.innerWidth - box.clientWidth;
            if (top >= window.innerHeight - box.clientHeight)
              top = window.innerHeight - box.clientHeight;
            box.setAttribute('style', `left:${left}px; top:${top}px;transform:none`);
          }
        }
    
        function moveStart(ev: MouseEvent) {
          dx = ev.clientX;
          dy = ev.clientY;
          const cssObj = box?.getBoundingClientRect();
          sx = cssObj?.left as number;
          sy = cssObj?.top as number;
          document.addEventListener('mousemove', move, false);
        }
        if (props.draggable) {
          if (moveEl.value) {
            box = moveEl.value.parentElement as HTMLElement;
            box.addEventListener('mousedown', moveStart, false);
          }
          document.addEventListener('mouseup', function () {
            if (moveEl.value) {
              document.removeEventListener('mousemove', move, false);
            }
          });
        }
      });
    </script>
    <style lang="less" scoped>
      .mask {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }
    
      .container {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }
    
      .confirm {
        position: absolute;
        top: 30%;
        left: 50%;
        transform: translate(-50%, 0);
        width: 427px;
        height: 151px;
        background-color: var(--color-bg-3);
        box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
        border: 1px solid var(--color-neutral-3);
        .close {
          position: absolute;
          width: 20px;
          height: 20px;
          border-radius: 50%;
          display: flex;
          justify-content: center;
          align-items: center;
          top: 12px;
          right: 16px;
          cursor: pointer;
          font-size: 12px;
          color: var(--color-text-1);
          transition: background-color 0.3s;
          &:hover {
            background-color: var(--color-fill-2);
            transition: background-color 0.3s;
          }
        }
      }
      .confirm[theme='dark'] {
        box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.4);
      }
    
      .head {
        display: flex;
        align-items: center;
        padding: 19px 23px 12px;
        width: 100%;
        font-size: 26px;
        color: rgb(255, 125, 0);
    
        .title {
          margin-left: 13px;
          font-size: 16px;
          font-weight: 500;
          color: var(--color-text-1);
          line-height: 32px;
        }
      }
      .move {
        cursor: move;
      }
      .msg {
        margin-left: 60px;
        margin-bottom: 23px;
        height: 19px;
        font-size: 14px;
        font-weight: 400;
        color: var(--color-text-1);
        line-height: 19px;
      }
    
      .footer {
        display: flex;
        justify-content: flex-end;
        padding-right: 16px;
        button {
          border: none;
          width: 85px;
          height: 31px;
          background-color: var(--color-secondary);
          border-radius: 4px;
          margin-left: 12px;
          color: var(--color-text-1);
          cursor: pointer;
        }
        .sure {
          color: #ffffff;
          background-color: rgb(var(--primary-6));
          transition: background-color 0.3s;
          &:hover {
            background-color: rgb(var(--primary-5));
            transition: background-color 0.3s;
          }
        }
      }
    </style>
    <style scoped></style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190

    API封装代码

    import { createVNode, render } from 'vue';
    import _Confirm from './confirm.vue';
    
    interface ConfigType {
      title: string;  // 标题
      content: string;  // 内容
      mask?: boolean;  // 蒙层
      draggable?: boolean;  // 是否可拖拽
      onOk?: () => void; // 确认回调
      onCancel?: () => void; // 取消回调
    }
    
    // 判断是不是函数类型
    function isFunction(arg: any): boolean {
      return typeof arg === 'function';
    }
    
    function open(config: ConfigType) {
      let container: HTMLElement | null = document.querySelector('.confirm-modal');
      if (!container) {
        container = document.createElement('div');
        container.className = 'confirm-modal';
        container.setAttribute(
          'style',
          `position: fixed;top: 0;left: 0;right: 0;bottom: 0;z-index: 1001;display:block`,
        );
      } else {
        container.style.zIndex = '1001';
        container.style.display = 'block';
      }
      
      // 销毁组件、移除container
      function hide() {
        // eslint-disable-next-line no-use-before-define
        if (vm.component) {
          // eslint-disable-next-line no-use-before-define
          vm.component.props.visible = false;
          if (container) {
            container.style.zIndex = '0';
            container.style.display = 'none';
          }
        }
        if (container) {
          render(null, container);
          document.body.removeChild(container);
        }
        container = null;
      }
    
      const handleOk = () => {
        if (config.onOk && isFunction(config.onOk)) {
          config?.onOk();
        }
        hide();
      };
    
      const handleCancel = () => {
        if (config.onCancel && isFunction(config.onCancel)) {
          config.onCancel();
        }
        hide();
      };
    
      const defaultConfig = {
        visible: true,
        mask: false,
        draggable: true,
      };
    
      const vm = createVNode(_Confirm, {
        ...defaultConfig,
        ...config,
        onOk: handleOk,
        onCancel: handleCancel,
      });
    
      render(vm, container);
      if (vm.component) {
        vm.component.props.visible = true;
      }
      if (!document.querySelector('.my-confirm')) {
        document.body.appendChild(container);
      }
    }
    export default {
      open,
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    封装得不是很好。大家轻点喷哈

  • 相关阅读:
    ZZ308 物联网应用与服务赛题第H套
    实践详解javap反编译类文件命令参数
    华为机试 - 面试
    LeetCode 2897. 对数组执行操作使平方和最大【贪心,位运算,哈希表】2301
    python免杀初探
    问题杂谈(三十八)处理Cesium双击定位后无法平移视角,只能旋转的问题
    怎么通过ssh连上ipv6的服务器?阿里云怎么配置ipv6?wsl2怎么支持ipv6?
    Spark集成ClickHouse(笔记)
    【Linux08-进程信号】信号的一生……
    前端性能优化 —— 使用 BMP 图片代替 canvas.toDataURL
  • 原文地址:https://blog.csdn.net/qq_32021429/article/details/133276894