当前位置: 代码网 > it编程>编程语言>Javascript > 前端实现界面元素拖拽功能的3种方式总结(亲测有效)

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

2025年02月13日 Javascript 我要评论
一、纯html+css+js实现;<!doctype html><html lang="en"><head> <meta charset="utf-8"&g

一、纯html+css+js实现;

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>document</title>
</head>
<body>
  <div id="draggabledialog">
    <div class="dialog-header">弹框标题</div>
    <div class="dialog-content">弹框内容</div>
</div>
</body>
<style>
  #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;
}
</style>
<script>
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;
});
</script>
</html>

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

二、vue模板实现;

<template>
  <div id="draggabledialog" ref="dialog">
    <div class="dialog-header" @mousedown="startdrag($event)">弹框标题</div>
    <div class="dialog-content">弹框内容</div>
  </div>
</template>
 
<script>
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);
    }
  }
};
</script>
 
<style>
#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;
}
</style>

原理同上;

三、vue全局指令实现;

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 < 0) left = 0;
                if (top < 0) top = 0;
                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种方式的文章就介绍到这了,更多相关前端实现界面元素拖拽内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com