• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    html5 拖拽及用 js 实现拖拽功能的示例代码

    发布者: 雪落无声 | 发布时间: 2025-6-16 12:30| 查看数: 81| 评论数: 0|帖子模式

    1. HTML5 拖拽

    1.1 相关知识

    拖拽元素:可以为元素添加
    1. draggable="true"
    复制代码
    来让元素能够被拖拽。
    拖拽元素的事件监听:(应用于拖拽元素)
         
      1. ondragstart
      复制代码
      当拖拽开始时调用   
      1. ondragleave
      复制代码
      当鼠标离开拖拽元素时调用   
      1. ondragend
      复制代码
      当拖拽结束时调用   
      1. ondrag
      复制代码
      整个拖拽过程都会调用
    目标元素:把元素A拖拽到元素B里,那么元素B就是目标元素。页面中任何一个元素都可以成为目标元素。
    目标元素的事件监听:(应用于目标元素)
         
      1. ondragenter
      复制代码
      当拖拽元素进入时调用   
      1. ondragover
      复制代码
      当拖拽元素停留在目标元素上时,就会连续一直触发(不管拖拽元素此时是移动还是不动的状态)   
      1. ondrop
      复制代码
      当在目标元素上松开鼠标时调用   
      1. ondragleave
      复制代码
      当鼠标离开目标元素时调用
    如果想让拖拽元素在目标元素里做点事情,就必须要在
    1. ondragover()
    复制代码
    里加
    1. event.preventDefault()
    复制代码
    这一行代码。
    1.2 拖拽基础
    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.3 将 A 在 B、C 之间拖拽
    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>
    复制代码
    效果展示

    2. 用 js 实现拖拽

    2.1 js 简单拖拽

    按下鼠标进行简单的拖拽。
    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.2 带效果的拖拽
    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>
    复制代码
    效果展示

    有时会卡顿,未解决…
    到此这篇关于html5 拖拽及用 js 实现拖拽的文章就介绍到这了,更多相关html5 拖拽内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/749464.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

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