• 【前端开发】可拖拽浮窗组件


    1. 组件:DragWindow

    <template>
      <div id="dragWindow" ref="dragBox" :style="{ width: width, height: height }">
        <div id="dragHead" @mousedown.stop="mouseDownHandler">
          <slot name="title"></slot>
        </div>
        <div id="dragBody">
          <slot name="content"></slot>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: "DragWindow",
      components: {},
      props: {
        title: {
          type: String,
          default: "",
        },
        width: {
          type: String,
          default: "300px",
        },
        height: {
          type: String,
          default: "100vh",
        },
      },
      data() {
        return {
          // 是否可以移动
          isMove: false,
    
          // 移动开始位置
          startPosition: {},
          // 元素当前位置
          currentPosition: {},
    
          // 拖拽元素
          dragHead: null,
        };
      },
      computed: {},
      created() {},
      mounted() {},
      methods: {
        mouseDownHandler(e) {
          // 获取元素当前位置
          this.currentPosition = {
            x: this.$refs.dragBox.offsetLeft,
            y: this.$refs.dragBox.offsetTop,
          };
    
          // 获取移动开始位置
          this.startPosition = {
            x: e.clientX,
            y: e.clientY,
          };
    
          // 设置为true,允许移动
          this.isMove = true;
    
          this.dragWindow = document.getElementById("dragWindow");
    
          // 鼠标移动事件
          this.dragWindow.addEventListener("mousemove", this.mouseMoveHandler);
          // 鼠标抬起事件
          this.dragWindow.addEventListener("mouseup", (e) => {
            this.isMove = false;
            this.dragWindow.removeEventListener(
                "mousemove",
                this.mouseMoveHandler
            );
          });
          // 鼠标移出事件
          this.dragWindow.addEventListener("mouseleave", (e) => {
            this.isMove = false;
            this.dragWindow.removeEventListener(
                "mousemove",
                this.mouseMoveHandler
            );
          });
        },
    
        // 鼠标移动事件
        mouseMoveHandler(e) {
          if (!this.isMove) {
            return;
          }
    
          // 获取鼠标移动的距离
          const nowX = e.clientX - this.startPosition.x,
              nowY = e.clientY - this.startPosition.y;
    
          // 计算并设置移动后的位置
          this.$refs.dragBox.style.left = `${this.currentPosition.x + nowX}px`;
          this.$refs.dragBox.style.top = `${this.currentPosition.y + nowY}px`;
        },
    
        // 关闭窗口
        close() {
          this.$emit("close");
        },
      },
    };
    </script>
    
    <style lang="scss" scoped>
    #dragWindow {
      position: fixed;
      left: 50%;
      top: 50%;
      z-index: 999;
      transform: translate(-50%, -50%);
      background-color: #030e06;
      color: #fff;
      border-radius: 10px;
    
      #dragHead {
        width: 100%;
        height: 50px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        background-color: rgba(#34a83a, 1);
        padding: 0px 10px 0px 20px;
        box-sizing: border-box;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        position: relative;
    
        .dragTitle {
          font-size: 18px;
        }
    
        .el-icon-close {
          font-size: 18px;
          cursor: pointer;
    
          &:hover {
            color: #ff6347;
          }
        }
      }
    
      #dragBody {
        width: 100%;
        height: calc(100% - 50px);
      }
    }
    </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

    2. main.js全局调用

    import DragWindow from './components/DragWindow/index.vue';
    
    • 1
        <DragWindow
            ref="DragWindow"
            w="600px"
            h="600px"
            title="可拖拽浮窗"
            @close="close()">
        </DragWindow>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    python经典百题之从小到大输出
    Istio(十一):向istio服务网格中引入虚拟机
    python经典百题之交换数组元素
    Elasticsearch 7.X版本常用语法语句
    加入自定义函数共享output数组功能
    【学习笔记】ARC150/ARC151
    机器学习课后习题 --- 朴素贝叶斯
    阿里推出新品牌“瓴羊”,致力成为“数字化领头羊”
    实现微信机器人开发,个微api
    java高校自习室座位预订系统springboot+vue
  • 原文地址:https://blog.csdn.net/weixin_48348920/article/details/138190783