网页技术交流
 
发新帖
楼主: 零五零八
查看: 673|回复: 0

[使用教程] html5 拖拽及用 js 实现拖拽功能的示例代码

[复制链接]
零五零八 发表于 2021-3-5 21:41:16 | 显示全部楼层
拖拽元素:可以为元素添加 draggable="true"来让元素能够被拖拽。
拖拽元素的事件监听:(应用于拖拽元素)
  • ondragstart当拖拽开始时调用
  • ondragleave 当鼠标离开拖拽元素时调用
  • ondragend 当拖拽结束时调用
  • ondrag 整个拖拽过程都会调用
目标元素:把元素A拖拽到元素B里,那么元素B就是目标元素。页面中任何一个元素都可以成为目标元素。
目标元素的事件监听:(应用于目标元素)
  • ondragenter 当拖拽元素进入时调用
  • ondragover 当拖拽元素停留在目标元素上时,就会连续一直触发(不管拖拽元素此时是移动还是不动的状态)
  • ondrop 当在目标元素上松开鼠标时调用
  • ondragleave 当鼠标离开目标元素时调用
如果想让拖拽元素在目标元素里做点事情,就必须要在 ondragover() 里加event.preventDefault()这一行代码。


  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8" />
  5.         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6.         <title>Document</title>
  7.         <style>
  8.             .box {
  9.                 width: 200px;
  10.                 height: 200px;
  11.                 background: green;
  12.             }
  13.             .box2 {
  14.                 position: relative;
  15.                 left: 300px;
  16.                 top: 50px;
  17.                 width: 300px;
  18.                 height: 300px;
  19.                 background: red;
  20.             }
  21.         </style>
  22.     </head>
  23.     <body>
  24.         <div class="box" draggable="true"></div>
  25.         <div class="box2"></div>
  26.         <script>
  27.             // HTML5 拖拽
  28.             // 应用于拖拽元素
  29.             var box = document.querySelector('.box')
  30.             box.ondragstart = function () {
  31.                 console.log('拖拽开始')
  32.             }
  33.             box.ondragleave = function () {
  34.                 console.log('鼠标离开元素')
  35.             }
  36.             box.ondragend = function () {
  37.                 console.log('拖拽结束')
  38.             }
  39.             // box.ondrag = function () {
  40.             //     console.log('在拖拽');
  41.             // }

  42.             // 应用于目标元素(想把 box 拖拽进去的地方)
  43.             var box2 = document.querySelector('.box2')
  44.             box2.ondragenter = function () {
  45.                 console.log('进来了')
  46.             }
  47.             box2.ondragleave = function () {
  48.                 console.log('离开了')
  49.             }

  50.             // 当拖拽元素在 目标元素上时,连续触发
  51.             box2.ondragover = function (e) {
  52.                 // 如果想让拖拽元素在目标元素里做点事情,就必须要在 ondragover() 里加event.preventDefault()这一行代码。
  53.                 e.preventDefault()
  54.                 console.log('over')
  55.             }
  56.             box2.ondrop = function () {
  57.                 console.log('松开鼠标了')
  58.             }
  59.         </script>
  60.     </body>
  61. </html>
复制代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8" />
  5.         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6.         <title>Document</title>
  7.         <style>
  8.             .box-b {
  9.                 width: 250px;
  10.                 height: 250px;
  11.                 background: green;
  12.             }
  13.             .cell-a {
  14.                 float: left;
  15.                 width: 50px;
  16.                 height: 50px;
  17.                 margin: 5px;
  18.                 text-align: center;
  19.                 line-height: 50px;
  20.                 border-radius: 50%;
  21.                 background: red;
  22.             }
  23.             .box-c {
  24.                 width: 200px;
  25.                 height: 200px;
  26.                 margin-top: 10px;
  27.                 background: skyblue;
  28.             }
  29.         </style>
  30.     </head>
  31.     <body>
  32.         <p>boxB</p>
  33.         <div class="box-b">
  34.             <div class="cell-a" draggable="true">1</div>
  35.             <div class="cell-a" draggable="true">2</div>
  36.             <div class="cell-a" draggable="true">3</div>
  37.             <div class="cell-a" draggable="true">4</div>
  38.             <div class="cell-a" draggable="true">5</div>
  39.         </div>
  40.         <p>boxC</p>
  41.         <div class="box-c"></div>
  42.         <script>
  43.             var cellA = document.querySelectorAll('.cell-a')
  44.             var boxB = document.querySelector('.box-b')
  45.             var boxC = document.querySelector('.box-c')
  46.             var temp = null

  47.             cellA.forEach((cell, index) => {
  48.                 // 从 boxB 拖拽到 boxC
  49.                 cell.ondragstart = function () {
  50.                     // 保持当前拖拽的元素
  51.                     temp = this
  52.                 }
  53.                 cell.ondragend = function () {
  54.                     temp = null
  55.                 }
  56.                 boxC.ondragover = function (e) {
  57.                     e.preventDefault()
  58.                 }
  59.                 boxC.ondragenter = function () {
  60.                     this.appendChild(temp)
  61.                 }

  62.                 // 从 boxC 拖拽到 boxB
  63.                 boxB.ondragover = function (e) {
  64.                     e.preventDefault()
  65.                 }
  66.                 boxB.ondragenter = function () {
  67.                     this.appendChild(temp)
  68.                 }
  69.             })
  70.         </script>
  71.     </body>
  72. </html>
复制代码

效果展示


1.gif


按下鼠标进行简单的拖拽。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8" />
  5.         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6.         <title>Document</title>
  7.         <style>
  8.             #box {
  9.                 position: absolute;
  10.                 width: 200px;
  11.                 height: 200px;
  12.                 background: green;
  13.             }
  14.         </style>
  15.         <script>
  16.             window.onload = function () {
  17.                 var box = document.getElementById('box')
  18.                 var disX = 0
  19.                 var disY = 0

  20.                 box.onmousedown = function (e) {
  21.                     var e = e || window.event
  22.                     disX = e.clientX - this.offsetLeft
  23.                     disY = e.clientY - this.offsetTop
  24.                     box.onmousemove = function (e) {
  25.                         var e = e || window.event
  26.                         box.style.left = e.clientX - disX + 'px'
  27.                         box.style.top = e.clientY - disY + 'px'
  28.                     }
  29.                     box.onmouseup = function (e) {
  30.                         console.log('end')
  31.                         this.onmousemove = null
  32.                     }
  33.                     return false
  34.                 }
  35.             }
  36.         </script>
  37.     </head>
  38.     <body>
  39.         <div id="box"></div>
  40.     </body>
  41. </html>
复制代码

效果展示

2.gif

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="UTF-8" />
  5.         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6.         <title>Document</title>
  7.         <style>
  8.             .box {
  9.                 position: absolute;
  10.                 width: 200px;
  11.                 height: 200px;
  12.                 background: skyblue;
  13.             }
  14.             .box1 {
  15.                 position: absolute;
  16.                 border: 1px dashed black;
  17.                 opacity: 0.5;
  18.             }
  19.             .way-box {
  20.                 position: absolute;
  21.                 bottom: 30px;
  22.                 right: 30px;
  23.                 /* 无法选中 */
  24.                 -moz-user-select: none; /* 火狐 */
  25.                 -webkit-user-select: none; /* 谷歌 */
  26.                 -ms-user-select: none; /* IE */
  27.                 user-select: none;
  28.             }
  29.         </style>
  30.         <script>
  31.             window.onload = function () {
  32.                 ;(function () {
  33.                     var box = document.querySelector('.box')
  34.                     var disX, disY, temp
  35.                     var body = document.querySelector('body')
  36.                     var way1 = document.querySelector('#way1')
  37.                     var way2 = document.querySelector('#way2')

  38.                     box.onmousedown = function (e) {
  39.                         var e = e || window.event // 兼容性写法
  40.                         disX = e.clientX - this.offsetLeft
  41.                         disY = e.clientY - this.offsetTop

  42.                         temp = document.createElement('div')
  43.                         body.appendChild(temp)
  44.                         temp.classList.add('box')
  45.                         temp.classList.add('box1')
  46.                         // 移动后位置会变,temp 的位置应该与 box 位置重合
  47.                         temp.style.left = e.clientX - disX + 'px' // 记得加单位!
  48.                         temp.style.top = e.clientY - disY + 'px'

  49.                         temp.onmousemove = function (e) {
  50.                             var e = e || window.event
  51.                             temp.style.left = e.clientX - disX + 'px' // 记得加单位!
  52.                             temp.style.top = e.clientY - disY + 'px'
  53.                         }
  54.                         temp.onmouseup = function (e) {
  55.                             console.log('end')
  56.                             this.onmousemove = null
  57.                             // 1 则默认不发生实际移动
  58.                             if (way2.checked) {
  59.                                 box.style.left = e.clientX - disX + 'px'
  60.                                 box.style.top = e.clientY - disY + 'px'
  61.                             }
  62.                             temp.style.display = 'none'
  63.                             this.onmouseup = null
  64.                         }
  65.                     }
  66.                 })()
  67.             }
  68.         </script>
  69.     </head>
  70.     <body>
  71.         <div class="box"></div>
  72.         <div class="way-box">
  73.             <p>请选择拖拽的方式</p>
  74.             <input type="radio" id="way1" name="way" checked />
  75.             <label for="way1">1</label>
  76.             <input type="radio" id="way2" name="way" />
  77.             <label for="way2">2</label>
  78.         </div>
  79.     </body>
  80. </html>
复制代码

效果展示

3.gif

快速回复 返回顶部 返回列表