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


Posted in HTML / CSS onOctober 23, 2020

1. HTML5 拖拽

1.1 相关知识

拖拽元素:可以为元素添加 draggable="true"来让元素能够被拖拽。

拖拽元素的事件监听:(应用于拖拽元素)

  • ondragstart当拖拽开始时调用
  • ondragleave 当鼠标离开拖拽元素时调用
  • ondragend 当拖拽结束时调用
  • ondrag 整个拖拽过程都会调用

目标元素:把元素A拖拽到元素B里,那么元素B就是目标元素。页面中任何一个元素都可以成为目标元素。

目标元素的事件监听:(应用于目标元素)

  • ondragenter 当拖拽元素进入时调用
  • ondragover 当拖拽元素停留在目标元素上时,就会连续一直触发(不管拖拽元素此时是移动还是不动的状态)
  • ondrop 当在目标元素上松开鼠标时调用
  • ondragleave 当鼠标离开目标元素时调用

如果想让拖拽元素在目标元素里做点事情,就必须要在 ondragover() 里加event.preventDefault()这一行代码。

1.2 拖拽基础

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .box {
                width: 200px;
                height: 200px;
                background: green;
            }
            .box2 {
                position: relative;
                left: 300px;
                top: 50px;
                width: 300px;
                height: 300px;
                background: red;
            }
        </style>
    </head>
    <body>
        <div class="box" draggable="true"></div>
        <div class="box2"></div>
        <script>
            // HTML5 拖拽
            // 应用于拖拽元素
            var box = document.querySelector('.box')
            box.ondragstart = function () {
                console.log('拖拽开始')
            }
            box.ondragleave = function () {
                console.log('鼠标离开元素')
            }
            box.ondragend = function () {
                console.log('拖拽结束')
            }
            // box.ondrag = function () {
            //     console.log('在拖拽');
            // }

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

            // 当拖拽元素在 目标元素上时,连续触发
            box2.ondragover = function (e) {
                // 如果想让拖拽元素在目标元素里做点事情,就必须要在 ondragover() 里加event.preventDefault()这一行代码。
                e.preventDefault()
                console.log('over')
            }
            box2.ondrop = function () {
                console.log('松开鼠标了')
            }
        </script>
    </body>
</html>

1.3 将 A 在 B、C 之间拖拽

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .box-b {
                width: 250px;
                height: 250px;
                background: green;
            }
            .cell-a {
                float: left;
                width: 50px;
                height: 50px;
                margin: 5px;
                text-align: center;
                line-height: 50px;
                border-radius: 50%;
                background: red;
            }
            .box-c {
                width: 200px;
                height: 200px;
                margin-top: 10px;
                background: skyblue;
            }
        </style>
    </head>
    <body>
        <p>boxB</p>
        <div class="box-b">
            <div class="cell-a" draggable="true">1</div>
            <div class="cell-a" draggable="true">2</div>
            <div class="cell-a" draggable="true">3</div>
            <div class="cell-a" draggable="true">4</div>
            <div class="cell-a" draggable="true">5</div>
        </div>
        <p>boxC</p>
        <div class="box-c"></div>
        <script>
            var cellA = document.querySelectorAll('.cell-a')
            var boxB = document.querySelector('.box-b')
            var boxC = document.querySelector('.box-c')
            var temp = null

            cellA.forEach((cell, index) => {
                // 从 boxB 拖拽到 boxC
                cell.ondragstart = function () {
                    // 保持当前拖拽的元素
                    temp = this
                }
                cell.ondragend = function () {
                    temp = null
                }
                boxC.ondragover = function (e) {
                    e.preventDefault()
                }
                boxC.ondragenter = function () {
                    this.appendChild(temp)
                }

                // 从 boxC 拖拽到 boxB
                boxB.ondragover = function (e) {
                    e.preventDefault()
                }
                boxB.ondragenter = function () {
                    this.appendChild(temp)
                }
            })
        </script>
    </body>
</html>

效果展示

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

2. 用 js 实现拖拽

2.1 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>
        <style>
            #box {
                position: absolute;
                width: 200px;
                height: 200px;
                background: green;
            }
        </style>
        <script>
            window.onload = function () {
                var box = document.getElementById('box')
                var disX = 0
                var disY = 0

                box.onmousedown = function (e) {
                    var e = e || window.event
                    disX = e.clientX - this.offsetLeft
                    disY = e.clientY - this.offsetTop
                    box.onmousemove = function (e) {
                        var e = e || window.event
                        box.style.left = e.clientX - disX + 'px'
                        box.style.top = e.clientY - disY + 'px'
                    }
                    box.onmouseup = function (e) {
                        console.log('end')
                        this.onmousemove = null
                    }
                    return false
                }
            }
        </script>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>

效果展示

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

2.2 带效果的拖拽

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            .box {
                position: absolute;
                width: 200px;
                height: 200px;
                background: skyblue;
            }
            .box1 {
                position: absolute;
                border: 1px dashed black;
                opacity: 0.5;
            }
            .way-box {
                position: absolute;
                bottom: 30px;
                right: 30px;
                /* 无法选中 */
                -moz-user-select: none; /* 火狐 */
                -webkit-user-select: none; /* 谷歌 */
                -ms-user-select: none; /* IE */
                user-select: none;
            }
        </style>
        <script>
            window.onload = function () {
                ;(function () {
                    var box = document.querySelector('.box')
                    var disX, disY, temp
                    var body = document.querySelector('body')
                    var way1 = document.querySelector('#way1')
                    var way2 = document.querySelector('#way2')

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

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

                        temp.onmousemove = function (e) {
                            var e = e || window.event
                            temp.style.left = e.clientX - disX + 'px' // 记得加单位!
                            temp.style.top = e.clientY - disY + 'px'
                        }
                        temp.onmouseup = function (e) {
                            console.log('end')
                            this.onmousemove = null
                            // 1 则默认不发生实际移动
                            if (way2.checked) {
                                box.style.left = e.clientX - disX + 'px'
                                box.style.top = e.clientY - disY + 'px'
                            }
                            temp.style.display = 'none'
                            this.onmouseup = null
                        }
                    }
                })()
            }
        </script>
    </head>
    <body>
        <div class="box"></div>
        <div class="way-box">
            <p>请选择拖拽的方式</p>
            <input type="radio" id="way1" name="way" checked />
            <label for="way1">1</label>
            <input type="radio" id="way2" name="way" />
            <label for="way2">2</label>
        </div>
    </body>
</html>

效果展示

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

有时会卡顿,未解决…

到此这篇关于html5 拖拽及用 js 实现拖拽的文章就介绍到这了,更多相关html5 拖拽内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
使用CSS3制作响应式导航菜单的方法
Jul 12 HTML / CSS
可自定义箭头样式的CSS3气泡提示框
Mar 16 HTML / CSS
CSS3圆角边框和边界图片效果实例
Jul 01 HTML / CSS
浅谈CSS3中的变形功能-transform功能
Dec 27 HTML / CSS
CSS3实现渐变背景兼容问题
May 06 HTML / CSS
HTML5 绘制图像(上)之:关于canvas元素引领下一代web页面的问题
Apr 24 HTML / CSS
HTML5 embed标签定义和用法详解
May 09 HTML / CSS
浅谈利用缓存来优化HTML5 Canvas程序的性能
May 12 HTML / CSS
HTML5拖拽的简单实例
May 30 HTML / CSS
HTML5实现直播间评论滚动效果的代码
May 27 HTML / CSS
CSS 一行代码实现头像与国旗的融合
Oct 24 HTML / CSS
css3手动实现pc端横向滚动
Jun 21 HTML / CSS
html5小程序飞入购物车(抛物线绘制运动轨迹点)
Oct 19 #HTML / CSS
app内嵌H5 webview 本地缓存问题的解决
Oct 19 #HTML / CSS
使用HTML5做的导航条详细步骤
Oct 19 #HTML / CSS
利用Node实现HTML5离线存储的方法
Oct 16 #HTML / CSS
HTML+CSS+JavaScript实现图片3D展览的示例代码
Oct 12 #HTML / CSS
HTML5逐步分析实现拖放功能的方法
Sep 30 #HTML / CSS
移动端HTML5 input常见问题(小结)
Sep 28 #HTML / CSS
You might like
ADODB类使用
2006/11/25 PHP
很好用的PHP数据库类
2009/05/27 PHP
关于php循环跳出的问题
2013/07/01 PHP
php过滤html标记属性类用法实例
2014/09/23 PHP
PHP http请求超时问题解决方案
2020/11/13 PHP
通过jquery实现tab标签浏览效果
2007/02/20 Javascript
服务器安全设置的几个注册表设置
2007/07/28 Javascript
js Date自定义函数 延迟脚本执行
2010/03/10 Javascript
JQuery的自定义事件代码,触发,绑定简单实例
2013/08/01 Javascript
js下拉菜单语言选项简单实现
2013/09/23 Javascript
JavaScript设计模式之建造者模式介绍
2014/12/28 Javascript
JS中捕获console.log()输出的方法
2015/04/16 Javascript
在Javascript中处理数组之toSource()方法的使用
2015/06/09 Javascript
浅析JavaScript访问对象属性和方法及区别
2015/11/16 Javascript
layer实现弹窗提交信息
2016/12/12 Javascript
jQuery ajax实现省市县三级联动
2021/03/07 Javascript
JS 组件系列之Bootstrap Table的冻结列功能彻底解决高度问题
2017/06/30 Javascript
JS模拟实现哈希表及应用详解
2018/05/04 Javascript
vue最简单的前后端交互示例详解
2018/10/11 Javascript
微信小程序使用map组件实现解析经纬度功能示例
2019/01/22 Javascript
[05:42]DOTA2英雄梦之声_第10期_蝙蝠骑士
2014/06/21 DOTA
Python警察与小偷的实现之一客户端与服务端通信实例
2014/10/09 Python
Python实现自动登录百度空间的方法
2017/06/10 Python
python DataFrame 修改列的顺序实例
2018/04/10 Python
Python 爬虫之Beautiful Soup模块使用指南
2018/07/05 Python
Python查找最长不包含重复字符的子字符串算法示例
2019/02/13 Python
在Python中表示一个对象的方法
2019/06/25 Python
python GUI库图形界面开发之PyQt5图片显示控件QPixmap详细使用方法与实例
2020/02/27 Python
linux系统下pip升级报错的解决方法
2021/01/31 Python
阿玛瑞酒店中文官方网站:Amari.com
2018/02/13 全球购物
会计工作心得体会
2014/01/13 职场文书
向国旗敬礼活动总结范文2014
2014/09/27 职场文书
安全员岗位职责
2015/02/10 职场文书
合作合同协议书
2016/03/21 职场文书
详解Js模块化的作用原理和方案
2021/04/29 Javascript
深入理解go缓存库freecache的使用
2022/02/15 Golang