Wang's blog Wang's blog
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Wang Mings

跟随大神,成为大神!
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • CSS

  • Npm

  • Vue

  • HTML

  • Node

  • Yaml

  • React

  • 框架

  • 规范

  • Electron

  • JS演示

    • drag拖拽
    • 原生js导出json为excel的三种方式
    • 使用原生js来替换title属性的悬浮文字提示
  • VuePress

  • JavaScript

  • TypeScript

  • 微信小程序

  • TypeScript-axios

  • 前端
  • JS演示
wangmings
2022-07-19

drag拖拽

# drag拖拽

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>拖拽</title>
    <style type="text/css">
      #box {
        position: fixed;
        left: 100px;
        top: 100px;
        padding: 5px;
        background: #f0f3f9;
        font-size: 12px;
      }
      #main {
        border: 1px solid #a0b3d6;
        background: white;
      }
      #bar {
        line-height: 24px;
        background: #beceeb;
        border-bottom: 1px solid #a0b3d6;
        padding-left: 5px;
        cursor: move;
      }
      #content {
        width: 420px;
        height: 250px;
        padding: 10px 5px;
      }
    </style>
  </head>

  <body>
    <div id="box" draggable="true">
      <div id="main">
        <div id="bar">拖拽</div>
        <div id="content">
          内容……
        </div>
      </div>
    </div>
    <script type="text/javascript">
    const box = document.getElementById('box')
    // 开始拖拽时鼠标跟拖拽元素位置的相差值
    let offset = {
      x: 0,
      y: 0
    }

    box.addEventListener("dragstart", function(event) {
      // 计算偏差s
      const rect = event.target.getBoundingClientRect()
      offset.x = event.clientX - rect.left
      offset.y = event.clientY - rect.top
      // 隐藏拖拽元素,必须异步处理
      setTimeout(() => {
        event.target.style.display = 'none'
      })
    }, false);

    box.addEventListener("dragend", function(event) {
      // 拖拽完成,显示
      event.target.style.display = 'block'
    }, false);

    // drop 的目标上触发
    document.addEventListener("dragover", function(event) {
      // 阻止默认行为以允许 drop
      event.preventDefault();
    }, false);

    document.addEventListener("drop", function(event) {
      // 阻止默认行为 (某些元素会作为链接打开)
      event.preventDefault();
      // 移动到鼠标的位置
      box.style.left = event.clientX - offset.x + 'px'
      box.style.top = event.clientY - offset.y + 'px'
    }, false);
    </script>
  </body>
</html>
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
编辑 (opens new window)
实战经验和技巧
原生js导出json为excel的三种方式

← 实战经验和技巧 原生js导出json为excel的三种方式→

最近更新
01
theme-vdoing-blog博客静态编译问题
09-16
02
搜索引擎
07-19
03
友情链接
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式