IT俱乐部 JavaScript 前端实现界面元素拖拽功能的3种方式总结(亲测有效)

前端实现界面元素拖拽功能的3种方式总结(亲测有效)

一、纯HTML+CSS+JS实现;

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
  <title>Document</title><div id="draggableDialog">
    <div class="dialog-header">弹框标题</div>
    <div class="dialog-content">弹框内容</div>
</div>
 
 
  #draggableDialog {
    position: absolute;
    width: 300px;
    height: 200px;
    background-color: #fff;
    border: 1px solid #ccc;
    padding: 10px;
}
.dialog-header {
    cursor: move;
    background-color: #f0f0f0;
    padding: 5px;
}
 
const dialog = document.getElementById('draggableDialog');
const dialogHeader = dialog.querySelector('.dialog-header');
let isDragging = false;
let offsetX, offsetY;
dialogHeader.addEventListener('mousedown', (e) => {
    isDragging = true;
    offsetX = e.clientX - dialog.offsetLeft;
    offsetY = e.clientY - dialog.offsetTop;
});
document.addEventListener('mousemove', (e) => {
    if (isDragging) {
        dialog.style.left = e.clientX - offsetX + 'px';
        dialog.style.top = e.clientY - offsetY + 'px';
    }
});
document.addEventListener('mouseup', () => {
    isDragging = false;
});

实现原理:使用js事件监听,监听document全局DOM的鼠标移动事件,当触发事件后,目标元素将会随鼠标一起偏移(移动)相对应的距离;因目标元素并非一直需要跟随鼠标移动,于是通过给目标元素指定区域dialogHeader 添加鼠标按下事件配合isDragging控制阀,实现只有当鼠标在指定区域按下才能实现拖动目标元素的效果。

二、VUE模板实现;

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
<div id="draggableDialog">
    <div class="dialog-header">弹框标题</div>
    <div class="dialog-content">弹框内容</div>
  </div>
 
export default {
  data() {
    return {
      isDragging: false,
      offsetX: 0,
      offsetY: 0
    };
  },
  methods: {
    startDrag(event) {
      this.isDragging = true;
      this.offsetX = event.clientX - this.$refs.dialog.offsetLeft;
      this.offsetY = event.clientY - this.$refs.dialog.offsetTop;
      document.addEventListener('mousemove', this.drag);
      document.addEventListener('mouseup', this.stopDrag);
    },
    drag(event) {
      if (this.isDragging) {
        this.$refs.dialog.style.left = event.clientX - this.offsetX + 'px';
        this.$refs.dialog.style.top = event.clientY - this.offsetY + 'px';
      }
    },
    stopDrag() {
      this.isDragging = false;
      document.removeEventListener('mousemove', this.drag);
      document.removeEventListener('mouseup', this.stopDrag);
    }
  }
};
 
#draggableDialog {
  position: absolute;
  width: 300px;
  height: 200px;
  background-color: #fff;
  border: 1px solid #ccc;
  padding: 10px;
}
  
.dialog-header {
  cursor: move;
  background-color: #f0f0f0;
  padding: 5px;
}

原理同上;

三、VUE全局指令实现;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Vue.directive('drag', {
    bind(el) {
        el.onmousedown = function(e) {
            let disX = e.clientX - el.offsetLeft;
            let disY = e.clientY - el.offsetTop;
            document.onmousemove = function(e) {
                let left = e.clientX - disX;
                let top = e.clientY - disY;
                if (left  document.documentElement.clientWidth - el.offsetWidth) left = document.documentElement.clientWidth - el.offsetWidth;
                if (top > document.documentElement.clientHeight - el.offsetHeight) top = document.documentElement.clientHeight - el.offsetHeight;
                el.style.left = left + 'px';
                el.style.top = top + 'px';
            };
            document.onmouseup = function() {
                document.onmousemove = null;
                document.onmouseup = null;
            };
        };
    }
});

总结 

到此这篇关于前端实现界面元素拖拽功能的3种方式的文章就介绍到这了,更多相关前端实现界面元素拖拽内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/navsub/js/14756.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部